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

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

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

// Define available WooCommerce attribute fields for export
$wc_fields = array(
    'attribute_id'  => __( 'Attribute ID', 'primelink' ),
    'name'          => __( 'Attribute Name', 'primelink' ),
    'slug'          => __( 'Slug', 'primelink' ),
    'taxonomy'      => __( 'Taxonomy Name', 'primelink' ),
    'type'          => __( 'Type', 'primelink' ),
    'order_by'      => __( 'Order By', 'primelink' ),
    'has_archives'  => __( 'Has Archives', 'primelink' ),
    'term_count'    => __( 'Term Count', 'primelink' ),
    'term_names'    => __( 'Term Names (comma-separated)', 'primelink' ),
    'term_slugs'    => __( 'Term Slugs (comma-separated)', 'primelink' ),
    'term_ids'      => __( 'Term IDs (comma-separated)', 'primelink' ),
    'terms'         => __( 'Terms (JSON)', '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. Attribute Name: name, attribute_name, attr_name).', '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(
                                'attribute_id'  => __( 'Attribute ID', 'primelink' ),
                                'name'          => __( 'Attribute Name', 'primelink' ),
                                'slug'          => __( 'Slug', 'primelink' ),
                                'taxonomy'      => __( 'Taxonomy Name', 'primelink' ),
                                'type'          => __( 'Type', 'primelink' ),
                                'order_by'      => __( 'Order By', 'primelink' ),
                                'has_archives'  => __( 'Has Archives', 'primelink' ),
                            ) ); ?>
                        </div>
                    </details>

                    <!-- Terms -->
                    <details class="mt_primelink-accordion">
                        <summary class="mt_primelink-accordion-title"><?php esc_html_e( 'Attribute Terms', 'primelink' ); ?></summary>
                        <div class="mt_primelink-accordion-body">
                            <?php TemplateHelper::render_export_slots( array(
                                'term_count'    => __( 'Term Count', 'primelink' ),
                                'term_names'    => __( 'Term Names (comma-separated)', 'primelink' ),
                                'term_slugs'    => __( 'Term Slugs (comma-separated)', 'primelink' ),
                                'term_ids'      => __( 'Term IDs (comma-separated)', 'primelink' ),
                                'terms'         => __( 'Terms (JSON)', 'primelink' ),
                            ) ); ?>
                            <p class="description"><?php esc_html_e( 'Terms can be exported as JSON (full details) or as comma-separated values (names, slugs, or IDs).', 'primelink' ); ?></p>
                        </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';
?>

