<?php
/**
 * New Import Wizard container.
 *
 * @package PrimeLink
 */

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

use PrimeLink\Modules\Imports\Controller\ImportWizardController;

$import_type = isset( $_GET['import_type'] ) ? sanitize_text_field( wp_unslash( $_GET['import_type'] ) ) : '';
$step        = ImportWizardController::get_step();
$import_id   = ImportWizardController::get_import_id();

// If import_id is provided but no import_type, try to get it from draft
if ( ! empty( $import_id ) && empty( $import_type ) ) {
    $drafts = get_option( 'mt_primelink_import_drafts', array() );
    if ( ! empty( $drafts[ $import_id ] ) ) {
        $draft = $drafts[ $import_id ];
        $draft_type = $draft['type'] ?? ( $draft['step_data'][1]['type'] ?? '' );
        
        // Normalize type for URL
        if ( $draft_type === 'product' ) {
            $import_type = 'products';
        } elseif ( $draft_type === 'order' ) {
            $import_type = 'orders';
        } elseif ( $draft_type === 'customer' ) {
            $import_type = 'customers';
        } elseif ( $draft_type === 'coupon' ) {
            $import_type = 'coupons';
        } elseif ( $draft_type === 'category' ) {
            $import_type = 'categories';
        } elseif ( $draft_type === 'tag' ) {
            $import_type = 'tags';
        } elseif ( $draft_type === 'attribute' ) {
            $import_type = 'attributes';
        } elseif ( $draft_type === 'variation' ) {
            $import_type = 'variations';
        } elseif ( $draft_type === 'review' ) {
            $import_type = 'reviews';
        } elseif ( $draft_type === 'download' ) {
            $import_type = 'downloads';
        } else {
            $import_type = $draft_type;
        }
        
        // If type is found and we're at step 1, redirect to step 2 (or step 5 if template is being loaded)
        if ( ! empty( $import_type ) && $step === 1 ) {
            $redirect_step = isset( $_GET['template'] ) ? 5 : 2;
            $redirect_url = add_query_arg( 
                array( 
                    'import_type' => $import_type,
                    'import_id'   => $import_id,
                    'step'        => $redirect_step,
                ),
                admin_url( 'admin.php?page=mt-primelink-new-import' )
            );
            if ( isset( $_GET['template'] ) ) {
                $redirect_url = add_query_arg( 'template', sanitize_text_field( wp_unslash( $_GET['template'] ) ), $redirect_url );
            }
            wp_safe_redirect( $redirect_url );
            exit;
        }
    }
}

// If no import_type is specified and no import_id, show the selection page
if ( empty( $import_type ) ) {
    include MT_PRIMELINK_PATH . 'templates/admin/imports/new-import-select.php';
    return;
}

$total = ImportWizardController::TOTAL_STEPS;

$base_url  = admin_url( 'admin.php?page=mt-primelink-new-import' );
if ( $import_type ) {
    $base_url = add_query_arg( 'import_type', $import_type, $base_url );
}

// Only show 'coming soon' for types that are not products, orders, customers, coupons, categories, tags, attributes, variations, reviews, or downloads
if ( ! in_array( $import_type, array( 'products', 'orders', 'customers', 'coupons', 'categories', 'tags', 'attributes', 'variations', 'reviews', 'downloads' ), true ) ) : ?>
    <div class="wrap mt-primelink-wrap mt-primelink-wizard">
        <div class="mt_primelink-import-modal glass" style="max-width:720px;margin:40px auto;">
            <div class="mt_primelink-import-modal__header">
                <h2><?php echo esc_html( ucfirst( $import_type ) . ' Import' ); ?></h2>
            </div>
            <p class="muted"><?php esc_html_e( 'This import type is coming soon. Please check back later.', 'primelink' ); ?></p>
            <div style="margin-top:12px;">
                <a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=mt-primelink-manage-import' ) ); ?>">
                    <?php esc_html_e( 'Back to Manage Imports', 'primelink' ); ?>
                </a>
            </div>
        </div>
    </div>
<?php return; endif; ?>
<div class="wrap mt-primelink-wrap mt-primelink-wizard">

    <div class="mt-primelink-wizard__header glass">
        <div class="mt_primelink-header-top-right">
            <div class="mt_primelink-wizard-version-inline mt_primelink-wizard-version-inline--top-right">
                <span class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Version', 'primelink' ); ?></span>
                <strong><?php echo esc_html( MT_PRIMELINK_VERSION ); ?></strong>
            </div>
            <a href="https://docs.malotop.ro/primelink/" target="_blank" rel="noopener" class="mt_primelink-docs-link" title="<?php esc_attr_e( 'Documentation & Support', 'primelink' ); ?>">
                <span class="dashicons dashicons-book-alt"></span>
                <span class="mt_primelink-docs-link-text"><?php esc_html_e( 'Docs', 'primelink' ); ?></span>
            </a>
        </div>
        <div class="mt-primelink-wizard__brand">
            <div class="mt_primelink-brand-head">
                <img src="<?php echo esc_url( MT_PRIMELINK_URL . 'assets/admin/img/logo.png' ); ?>" alt="<?php esc_attr_e( 'PrimeLink', 'primelink' ); ?>" class="mt_primelink-brand-logo" />
            </div>
            <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Guided Setup', 'primelink' ); ?></div>
            <h1><?php esc_html_e( 'New Import', 'primelink' ); ?></h1>
            <p class="muted"><?php esc_html_e( 'Follow the steps to configure your import.', 'primelink' ); ?></p>
        </div>

        <div class="mt-primelink-wizard__steps">
            <?php for ( $i = 1; $i <= $total; $i++ ) :
                $active = ( $i === $step ) ? 'is-active' : '';
                $done   = ( $i < $step ) ? 'is-done' : '';
            ?>
                <a class="mt-primelink-step <?php echo esc_attr( "$active $done" ); ?>"
                   href="<?php echo esc_url( add_query_arg( array( 'step' => $i, 'import_id' => $import_id ), $base_url ) ); ?>">
                    <span class="mt-primelink-step__num"><?php echo (int) $i; ?></span>
                    <span class="mt-primelink-step__label">
                        <?php echo esc_html( sprintf( __( 'Step %d', 'primelink' ), $i ) ); ?>
                    </span>
                </a>
            <?php endfor; ?>
        </div>
    </div>

    <div class="mt-primelink-wizard__body glass">
        <?php if ( ! class_exists( 'WooCommerce' ) ) : ?>
            <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Step 1 · Kickoff', 'primelink' ); ?></div>
            <h2><?php esc_html_e( 'WooCommerce is required for imports', 'primelink' ); ?></h2>
            <p><?php esc_html_e( 'PrimeLink needs WooCommerce to be installed and active in order to import products, orders, and other store data.', 'primelink' ); ?></p>
            <div class="mt-primelink-card muted">
                <p><?php esc_html_e( 'Please install and activate WooCommerce, then return to this wizard.', 'primelink' ); ?></p>
                <p style="margin-top:8px;">
                    <a href="<?php echo esc_url( admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' ) ); ?>" class="button button-primary">
                        <?php esc_html_e( 'Search WooCommerce in plugins', 'primelink' ); ?>
                    </a>
                    <a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" class="button">
                        <?php esc_html_e( 'Go to Plugins page', 'primelink' ); ?>
                    </a>
                </p>
            </div>
        <?php else : ?>
            <?php
            if ( $import_type === 'orders' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/orders/step-{$step}.php";
            } elseif ( $import_type === 'customers' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/customers/step-{$step}.php";
            } elseif ( $import_type === 'coupons' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/coupons/step-{$step}.php";
            } elseif ( $import_type === 'categories' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/categories/step-{$step}.php";
            } elseif ( $import_type === 'tags' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/tags/step-{$step}.php";
            } elseif ( $import_type === 'attributes' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/attributes/step-{$step}.php";
            } elseif ( $import_type === 'variations' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/variations/step-{$step}.php";
            } elseif ( $import_type === 'reviews' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/reviews/step-{$step}.php";
            } elseif ( $import_type === 'downloads' ) {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/downloads/step-{$step}.php";
            } else {
                $step_file = MT_PRIMELINK_PATH . "templates/admin/imports/partials/step-{$step}.php";
            }
            if ( file_exists( $step_file ) ) {
                include $step_file;
            } else {
                echo '<p>' . esc_html__( 'Step template missing.', 'primelink' ) . '</p>';
            }
            ?>
        <?php endif; ?>
    </div>

    <?php if ( class_exists( 'WooCommerce' ) ) : ?>
        <div class="mt-primelink-wizard__footer">
            <div class="left">
                <?php if ( $step > 1 ) : ?>
                    <a class="button" id="mt-primelink-back"
                       href="<?php echo esc_url( add_query_arg( array( 'step' => $step - 1, 'import_id' => $import_id ), $base_url ) ); ?>">
                        <?php esc_html_e( 'Back', 'primelink' ); ?>
                    </a>
                <?php endif; ?>
            </div>

            <div class="right">
                <?php if ( $step < $total ) : ?>
                    <div class="mt_primelink-next-wrap">
                        <button class="button button-primary" id="mt-primelink-next"
                                data-next="<?php echo (int) ( $step + 1 ); ?>"
                                data-import-id="<?php echo (int) $import_id; ?>">
                            <?php esc_html_e( 'Next', 'primelink' ); ?>
                        </button>
                        <span class="mt_primelink-save-status" id="mt_primelink-save-status" aria-live="polite"></span>
                    </div>
                <?php else : ?>
                    <button class="button button-primary" id="mt-primelink-finish"
                            data-import-id="<?php echo (int) $import_id; ?>">
                        <?php esc_html_e( 'Run Import', 'primelink' ); ?>
                    </button>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>
</div>
