<?php
/**
 * Step 2 - Field Mapping for Coupon Export.
 *
 * @package PrimeLink
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$export_id = isset( $_GET['export_id'] ) ? absint( $_GET['export_id'] ) : 0;

// Define available WooCommerce coupon fields for export
$wc_fields = array(
    'coupon_id'         => __( 'Coupon ID', 'primelink' ),
    'code'              => __( 'Coupon Code', 'primelink' ),
    'discount_type'     => __( 'Discount Type', 'primelink' ),
    'amount'            => __( 'Discount Amount', 'primelink' ),
    'description'       => __( 'Description', 'primelink' ),
    'date_expires'      => __( 'Date Expires', 'primelink' ),
    'usage_count'       => __( 'Usage Count', 'primelink' ),
    'usage_limit'       => __( 'Usage Limit', 'primelink' ),
    'usage_limit_per_user' => __( 'Usage Limit Per User', 'primelink' ),
    'minimum_amount'    => __( 'Minimum Amount', 'primelink' ),
    'maximum_amount'    => __( 'Maximum Amount', 'primelink' ),
    'individual_use'    => __( 'Individual Use', 'primelink' ),
    'exclude_sale_items' => __( 'Exclude Sale Items', 'primelink' ),
    'free_shipping'     => __( 'Free Shipping', 'primelink' ),
    'product_ids'       => __( 'Product IDs', 'primelink' ),
    'excluded_product_ids' => __( 'Excluded Product IDs', 'primelink' ),
    'product_categories' => __( 'Product Categories', 'primelink' ),
    'excluded_product_categories' => __( 'Excluded Product Categories', 'primelink' ),
    'customer_emails'   => __( 'Customer Emails', 'primelink' ),
    'limit_usage_to_x_items' => __( 'Limit Usage to X Items', 'primelink' ),
    'status'            => __( 'Status', 'primelink' ),
    'date_created'      => __( 'Date Created', 'primelink' ),
    'date_modified'     => __( 'Date Modified', 'primelink' ),
);

use PrimeLink\Helpers\TemplateHelper;
?>
<div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Step 2 · Mapping', 'primelink' ); ?></div>
<h2><?php esc_html_e( 'Field Mapping Studio', 'primelink' ); ?></h2>
<input type="hidden" id="mt-primelink-step" value="2">

<?php
// Include reusable templates section
include MT_PRIMELINK_PATH . 'templates/admin/exports/partials/templates-section.php';

// Include mapping shell (with alias card)
$alias_hint = __( 'Add custom synonyms for export columns (e.g. Coupon Code: code, coupon_code, discount_code).', 'primelink' );
include MT_PRIMELINK_PATH . 'templates/admin/exports/partials/mapping-shell.php';
?>

            <?php
            // Include export columns header (with helper row)
            include MT_PRIMELINK_PATH . 'templates/admin/exports/partials/export-columns-header.php';
            ?>

                    <!-- Basic Fields -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Basic Fields', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'coupon_id'     => __( 'Coupon ID', 'primelink' ),
                                'code'          => __( 'Coupon Code', 'primelink' ),
                                'discount_type' => __( 'Discount Type', 'primelink' ),
                                'amount'        => __( 'Discount Amount', 'primelink' ),
                                'description'   => __( 'Description', 'primelink' ),
                                'status'        => __( 'Status', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Usage Limits -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Usage Limits', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'usage_count'       => __( 'Usage Count', 'primelink' ),
                                'usage_limit'       => __( 'Usage Limit', 'primelink' ),
                                'usage_limit_per_user' => __( 'Usage Limit Per User', 'primelink' ),
                                'limit_usage_to_x_items' => __( 'Limit Usage to X Items', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Amount Limits -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Amount Limits', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'minimum_amount' => __( 'Minimum Amount', 'primelink' ),
                                'maximum_amount' => __( 'Maximum Amount', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Restrictions -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Restrictions', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'individual_use'    => __( 'Individual Use', 'primelink' ),
                                'exclude_sale_items' => __( 'Exclude Sale Items', 'primelink' ),
                                'free_shipping'     => __( 'Free Shipping', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Product Restrictions -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Product Restrictions', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'product_ids'       => __( 'Product IDs', 'primelink' ),
                                'excluded_product_ids' => __( 'Excluded Product IDs', 'primelink' ),
                                'product_categories' => __( 'Product Categories', 'primelink' ),
                                'excluded_product_categories' => __( 'Excluded Product Categories', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Customer Restrictions -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Customer Restrictions', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'customer_emails' => __( 'Customer Emails', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Dates -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Dates', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'date_expires'  => __( 'Date Expires', 'primelink' ),
                                'date_created'  => __( 'Date Created', 'primelink' ),
                                'date_modified' => __( 'Date Modified', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Custom Export Columns -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Custom Columns', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <div id="mt_primelink-custom-export-columns-wrap"></div>
                            <button type="button" class="button" id="mt_primelink-add-custom-export-column"><?php esc_html_e( 'Add Custom Column', 'primelink' ); ?></button>
                            <p class="description"><?php esc_html_e( 'Create custom export columns with custom headers.', 'primelink' ); ?></p>
                        </div>
                    </details>
                    
                    <?php
                    // Shared integrations mapping (Yoast, ACF, WPML, Polylang).
                    include MT_PRIMELINK_PATH . 'templates/admin/exports/partials/integrations-mapping.php';
                    ?>
                </div>
            </section>

            <!-- Preview -->
            <div id="mt-primelink-export-preview"
                 class="mt_primelink-preview-card"
                 data-export-id="<?php echo esc_attr( $export_id ); ?>">
            </div>
        </main>
    </div>
</div>

<?php
// Include reusable row filters
include MT_PRIMELINK_PATH . 'templates/admin/exports/partials/row-filters.php';
?>

<?php
// Include reusable map editor partial
include MT_PRIMELINK_PATH . 'templates/admin/exports/partials/map-editor.php';
?>

