<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * @param object                       $form
 * @param array<int,array<string,mixed>> $fields
 * @param array<string,string>         $messages
 * @param string                       $page_url
 * @param string                       $page_title
 * @param bool                         $show_title
 * @param string                       $plugin_name Optional marketplace plugin name for context.
 * @return void
 */
function algo_forms_render_markup($form, $fields, $messages, $page_url, $page_title, $show_title = false, $plugin_name = '')
{
    $form_title = (string) $form->form_title;
    $submit_label = $messages['submit_label'] ?? __('Submit', 'algo-forms');
    $sending_label = $messages['sending_label'] ?? __('Sending…', 'algo-forms');
    $error_generic = $messages['error_generic'] ?? __('Something went wrong. Please try again.', 'algo-forms');
    $plugin_name = trim((string) $plugin_name);
    ?>
<form class="algo-form" data-form-id="<?php echo esc_attr((string) $form->id); ?>"
    data-sending-label="<?php echo esc_attr($sending_label); ?>"
    data-error-generic="<?php echo esc_attr($error_generic); ?>">
    <input type="hidden" name="page_url" value="<?php echo esc_url($page_url); ?>">
    <input type="hidden" name="page_title" value="<?php echo esc_attr($page_title); ?>">
    <input type="hidden" name="af_form_token" value="<?php echo esc_attr(algo_forms_form_token((int) $form->id)); ?>">

    <?php if ($show_title && $form_title !== '') : ?>
    <h3 class="algo-form__title"><?php echo esc_html($form_title); ?></h3>
    <?php endif; ?>

    <div class="algo-form__fields">
    <?php if ($plugin_name !== '') : ?>
        <div class="algo-form__field algo-form__field--text algo-form__field--plugin">
            <label class="algo-form__label" for="af_plugin_name">
                <?php esc_html_e('Plugin', 'algo-forms'); ?>
            </label>
            <input class="algo-form__control" type="text" id="af_plugin_name"
                name="plugin_name" value="<?php echo esc_attr($plugin_name); ?>" readonly>
        </div>
    <?php endif; ?>
    <?php foreach ($fields as $index => $field) :
        $field_key = algo_forms_field_key($field, $index);
        $field_label = trim((string) ($field['label'] ?? ''));
        $field_type = sanitize_key((string) ($field['type'] ?? 'text'));
        if ($field_type === '') {
            $field_type = 'text';
        }
        ?>
        <div class="algo-form__field algo-form__field--<?php echo esc_attr($field_type); ?>">
            <?php if ($field_label !== '') : ?>
            <label class="algo-form__label" for="<?php echo esc_attr($field_key); ?>">
                <?php echo esc_html($field_label); ?>
                <?php if (!empty($field['required'])) : ?>
                <span class="algo-form__required" aria-hidden="true">*</span>
                <?php endif; ?>
            </label>
            <?php endif; ?>

            <?php if ($field_type === 'textarea') : ?>
            <textarea class="algo-form__control" id="<?php echo esc_attr($field_key); ?>"
                name="<?php echo esc_attr($field_key); ?>" rows="4"
                <?php echo !empty($field['required']) ? 'required' : ''; ?>
                <?php echo $field_label === '' ? ' aria-label="' . esc_attr($form_title) . '"' : ''; ?>></textarea>
            <?php elseif ($field_type === 'select') : ?>
            <select class="algo-form__control" id="<?php echo esc_attr($field_key); ?>"
                name="<?php echo esc_attr($field_key); ?>"
                <?php echo !empty($field['required']) ? 'required' : ''; ?>
                <?php echo $field_label === '' ? ' aria-label="' . esc_attr($form_title) . '"' : ''; ?>>
                <?php if (empty($field['required'])) : ?>
                <option value=""><?php esc_html_e('Select…', 'algo-forms'); ?></option>
                <?php endif; ?>
                <?php
                $options = isset($field['options']) && is_array($field['options']) ? $field['options'] : array();
                foreach ($options as $option) :
                    $option = trim((string) $option);
                    if ($option === '') {
                        continue;
                    }
                    ?>
                <option value="<?php echo esc_attr($option); ?>"><?php echo esc_html($option); ?></option>
                <?php endforeach; ?>
            </select>
            <?php else : ?>
            <input class="algo-form__control" type="<?php echo esc_attr($field_type); ?>"
                id="<?php echo esc_attr($field_key); ?>" name="<?php echo esc_attr($field_key); ?>"
                <?php echo !empty($field['required']) ? 'required' : ''; ?>
                <?php echo $field_label === '' ? ' aria-label="' . esc_attr($form_title) . '"' : ''; ?> />
            <?php endif; ?>
        </div>
    <?php endforeach; ?>
    </div>

    <?php
    if (function_exists('algo_forms_render_spam_fields')) {
        algo_forms_render_spam_fields();
    }
    ?>

    <div class="algo-form__actions">
        <button type="submit" class="algo-form__submit">
            <span class="algo-form__submit-label"><?php echo esc_html($submit_label); ?></span>
        </button>
    </div>
</form>
    <?php
}
