<?php
/**
 * Form Builder UI — shared partial for admin + vendor
 *
 * Variables expected:
 *   $role  string  'admin' or 'vendor'
 *
 * This file outputs:
 *   1. The mount point <div id="wahrb-form-builder-root">
 *   2. wp_localize_script() for the builder JS
 *
 * @package WahraRentalsBookings
 * @since   2.3.0
 */

defined('ABSPATH') || exit;

if (empty($role)) {
    $role = 'admin';
}

$vendor_id        = get_current_user_id();
$mode             = wahrb_get_mode(); // 'dokan' or 'solo'
$is_marketplace   = ($mode === 'dokan');

// Field type data from FormBuilder class
$field_types        = WAHRB_Form_Builder::get_field_types();        // flat array
$field_type_groups  = WAHRB_Form_Builder::get_field_types_grouped(); // grouped
$allowed_scopes     = WAHRB_Form_Builder::get_allowed_scopes($role); // role-specific scopes
$categories         = WAHRB_Platform_Categories::get_js_config();    // platform categories

// i18n strings for the JS builder
$i18n = [
    'formTemplates'   => __('Form Templates', 'wahra-rentals-bookings'),
    'createNew'       => __('Create New', 'wahra-rentals-bookings'),
    'name'            => __('Name', 'wahra-rentals-bookings'),
    'scope'           => __('Scope', 'wahra-rentals-bookings'),
    'category'        => __('Category', 'wahra-rentals-bookings'),
    'fields'          => __('fields', 'wahra-rentals-bookings'),
    'status'          => __('Status', 'wahra-rentals-bookings'),
    'actions'         => __('Actions', 'wahra-rentals-bookings'),
    'active'          => __('Active', 'wahra-rentals-bookings'),
    'inactive'        => __('Inactive', 'wahra-rentals-bookings'),
    'edit'            => __('Edit', 'wahra-rentals-bookings'),
    'delete'          => __('Delete', 'wahra-rentals-bookings'),
    'readOnly'        => __('Read-only', 'wahra-rentals-bookings'),
    'noTemplates'     => __('No form templates yet. Create your first one!', 'wahra-rentals-bookings'),
    'loading'         => __('Loading…', 'wahra-rentals-bookings'),
    'back'            => __('Back', 'wahra-rentals-bookings'),
    'save'            => __('Save', 'wahra-rentals-bookings'),
    'preview'         => __('Preview', 'wahra-rentals-bookings'),
    'editTemplate'    => __('Edit Template', 'wahra-rentals-bookings'),
    'newTemplate'     => __('New Template', 'wahra-rentals-bookings'),
    'templateName'    => __('Template Name', 'wahra-rentals-bookings'),
    'enterName'       => __('Enter template name…', 'wahra-rentals-bookings'),
    'description'     => __('Description', 'wahra-rentals-bookings'),
    'optional'        => __('Optional', 'wahra-rentals-bookings'),
    'selectCategory'  => __('Select…', 'wahra-rentals-bookings'),
    'productId'       => __('Product ID', 'wahra-rentals-bookings'),
    'fieldTypes'      => __('Field Types', 'wahra-rentals-bookings'),
    'formFields'      => __('Form Fields', 'wahra-rentals-bookings'),
    'fieldProperties' => __('Field Properties', 'wahra-rentals-bookings'),
    'selectField'     => __('Click a field in the canvas to edit its properties', 'wahra-rentals-bookings'),
    'dragFieldsHere'  => __('Drag fields here from the palette', 'wahra-rentals-bookings'),
    'fieldKey'        => __('Field Key', 'wahra-rentals-bookings'),
    'label'           => __('Label', 'wahra-rentals-bookings'),
    'required'        => __('Required', 'wahra-rentals-bookings'),
    'placeholder'     => __('Placeholder', 'wahra-rentals-bookings'),
    'defaultValue'    => __('Default Value', 'wahra-rentals-bookings'),
    'min'             => __('Min', 'wahra-rentals-bookings'),
    'max'             => __('Max', 'wahra-rentals-bookings'),
    'options'         => __('Options', 'wahra-rentals-bookings'),
    'addOption'       => __('Add Option', 'wahra-rentals-bookings'),
    'htmlContent'     => __('HTML Content', 'wahra-rentals-bookings'),
    'consentText'     => __('Consent Text', 'wahra-rentals-bookings'),
    'consentUrl'      => __('Terms URL', 'wahra-rentals-bookings'),
    'formPreview'     => __('Form Preview', 'wahra-rentals-bookings'),
    'backToBuilder'   => __('Back to Builder', 'wahra-rentals-bookings'),
    'noFieldsPreview' => __('No fields to preview. Add some fields in the builder.', 'wahra-rentals-bookings'),
    'removeField'     => __('Remove field', 'wahra-rentals-bookings'),
    'untitled'        => __('(Untitled)', 'wahra-rentals-bookings'),
    'confirmDelete'   => __('Are you sure you want to delete this template?', 'wahra-rentals-bookings'),
    'templateDeleted' => __('Template deleted.', 'wahra-rentals-bookings'),
    'templateSaved'   => __('Template saved successfully.', 'wahra-rentals-bookings'),
    'nameRequired'    => __('Template name is required.', 'wahra-rentals-bookings'),
    'fieldsRequired'  => __('Add at least one field.', 'wahra-rentals-bookings'),
    'unknownError'    => __('An error occurred', 'wahra-rentals-bookings'),
    'select'          => __('Select...', 'wahra-rentals-bookings'),
    'viewTerms'       => __('View terms', 'wahra-rentals-bookings'),
    'scopeGlobal'     => __('Global', 'wahra-rentals-bookings'),
    'scopeCategory'   => __('Category', 'wahra-rentals-bookings'),
    'scopeProduct'    => __('Product', 'wahra-rentals-bookings'),
    'scopeVendor'     => __('Vendor', 'wahra-rentals-bookings'),
];

// Localize the script (wahrb-form-builder-js handle must be enqueued before this partial is included)
wp_localize_script('wahrb-form-builder-js', 'wahrbFormBuilderConfig', [
    'apiUrl'          => untrailingslashit(rest_url('wahrb/v1')),
    'nonce'           => wp_create_nonce('wp_rest'),
    'role'            => $role,
    'mode'            => $mode,
    'vendorId'        => $vendor_id,
    'fieldTypes'      => $field_types,
    'fieldTypeGroups' => $field_type_groups,
    'allowedScopes'   => $allowed_scopes,
    'categories'      => $categories,
    'i18n'            => $i18n,
]);
?>

<div id="wahrb-form-builder-root" class="wahrb-fb-root" data-role="<?php echo esc_attr($role); ?>"></div>
