<?php

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

/** @var string $settings_url */
/** @var string $logs_url */

// Récupérer l'état des settings
$settings_status = \ESI_PEPPOL\controllers\PEPPOL_Plugin::check_settings_status();
?>

<div class="wrap esi-peppol-dashboard-wrap">
    <h1><?php esc_html_e('ESI Peppol - Passerelle WooCommerce', 'esi_peppol'); ?></h1>

    <p class="esi-peppol-dashboard-intro">
        <?php esc_html_e(
            'Ce plugin permet d\'envoyer vos documents de facturation WooCommerce vers le réseau Peppol. 
            Suivez les étapes ci-dessous pour configurer votre connexion.',
            'esi_peppol'
        ); ?>
    </p>

    <!-- Setup guidé - Timeline -->
    <div class="esi-peppol-setup-guide">
        <h2 style="margin-bottom: 30px;"><?php esc_html_e('Configuration du connecteur', 'esi_peppol'); ?></h2>
        
        <!-- Étape 1: Numéro TVA boutique -->
        <div class="step <?php echo $settings_status['vat_number']['completed'] ? 'is-done' : ''; ?>" 
             data-step="<?php echo $settings_status['vat_number']['completed'] ? '✓' : '1'; ?>">
            <div class="step-card">
                <h3><?php echo esc_html($settings_status['vat_number']['label']); ?></h3>
                <p><?php echo esc_html($settings_status['vat_number']['description']); ?></p>
                <?php if ($settings_status['vat_number']['completed']) : ?>
                    <div class="step-value">
                        <strong><?php esc_html_e('Valeur configurée :', 'esi_peppol'); ?></strong>
                        <code><?php echo esc_html($settings_status['vat_number']['value']); ?></code>
                    </div>
                <?php else : ?>
                    <a href="<?php echo esc_url($settings_status['vat_number']['action_url']); ?>" class="button button-primary">
                        <?php echo esc_html($settings_status['vat_number']['action_label']); ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Étape 2: Champ TVA client -->
        <div class="step <?php echo $settings_status['vat_field']['completed'] ? 'is-done' : ''; ?>" 
             data-step="<?php echo $settings_status['vat_field']['completed'] ? '✓' : '2'; ?>">
            <div class="step-card">
                <h3><?php echo esc_html($settings_status['vat_field']['label']); ?></h3>
                <p><?php echo esc_html($settings_status['vat_field']['description']); ?></p>
                <?php if ($settings_status['vat_field']['completed']) : ?>
                    <div class="step-value">
                        <strong><?php esc_html_e('Champ détecté :', 'esi_peppol'); ?></strong>
                        <code><?php echo esc_html($settings_status['vat_field']['value']); ?></code>
                    </div>
                <?php else : ?>
                    <a href="<?php echo esc_url($settings_status['vat_field']['action_url']); ?>" class="button button-primary">
                        <?php echo esc_html($settings_status['vat_field']['action_label']); ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Étape 3: Format num facture -->
        <div class="step <?php echo $settings_status['invoice_number_format']['completed'] ? 'is-done' : ''; ?>" 
             data-step="<?php echo $settings_status['invoice_number_format']['completed'] ? '✓' : '3'; ?>">
            <div class="step-card">
                <h3><?php echo esc_html($settings_status['invoice_number_format']['label']); ?></h3>
                <p><?php echo esc_html($settings_status['invoice_number_format']['description']); ?></p>
                <?php if ($settings_status['invoice_number_format']['completed']) : ?>
                    <div class="step-value">
                        <strong><?php esc_html_e('Valeur :', 'esi_peppol'); ?></strong>
                        <code><?php echo esc_html($settings_status['invoice_number_format']['value']); ?></code>
                    </div>
                <?php else : ?>
                    <a href="<?php echo esc_url($settings_status['invoice_number_format']['action_url']); ?>" class="button button-primary">
                        <?php echo esc_html($settings_status['invoice_number_format']['action_label']); ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Étape 4: Identifiants API -->
        <div class="step <?php echo $settings_status['api_credentials']['completed'] ? 'is-done' : ''; ?>" 
             data-step="<?php echo $settings_status['api_credentials']['completed'] ? '✓' : '4'; ?>">
            <div class="step-card">
                <h3><?php echo esc_html($settings_status['api_credentials']['label']); ?></h3>
                <p><?php echo esc_html($settings_status['api_credentials']['description']); ?></p>
                <?php if ($settings_status['api_credentials']['completed']) : ?>
                    <div class="step-value">
                        <strong><?php esc_html_e('Clé API configurée :', 'esi_peppol'); ?></strong>
                        <code><?php echo esc_html($settings_status['api_credentials']['value']); ?></code>
                    </div>
                <?php else : ?>
                    <a href="<?php echo esc_url($settings_status['api_credentials']['action_url']); ?>" class="button button-primary">
                        <?php echo esc_html($settings_status['api_credentials']['action_label']); ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Étape 5: Webhook -->
        <div class="step is-done" data-step="5">
            <div class="step-card">
                <h3><?php echo esc_html($settings_status['webhook']['label']); ?></h3>
                <p><?php echo esc_html($settings_status['webhook']['description']); ?></p>
                <div class="esi-peppol-webhook-display">
                    <code class="esi-peppol-webhook-url"><?php echo esc_html($settings_status['webhook']['value']); ?></code>
                    <button type="button" class="button button-small esi-peppol-copy-webhook-url" data-copy-text="<?php echo esc_attr($settings_status['webhook']['value']); ?>">
                        <?php esc_html_e('Copier', 'esi_peppol'); ?>
                    </button>
                </div>
                <p class="step-note">
                    <?php
                    printf(
                        wp_kses_post(
                            /* translators: %s: URL de la documentation */
                            __('Configurez cette URL dans votre profil d\'entreprise sur <a href="%s" target="_blank" rel="noopener noreferrer">https://peppol.esi-web.be/</a>', 'esi_peppol')
                        ),
                        esc_url('https://peppol.esi-web.be/')
                    );
                    ?>
                </p>
            </div>
        </div>

        <!-- Étape 6: Email (optionnel) -->
        <div class="step <?php echo $settings_status['email']['completed'] ? 'is-done' : ''; ?>" 
             data-step="<?php echo $settings_status['email']['completed'] ? '✓' : '6'; ?>">
            <div class="step-card">
                <h3>
                    <?php echo esc_html($settings_status['email']['label']); ?>
                    <span style="font-size: 12px; font-weight: normal; opacity: 0.8;">(<?php esc_html_e('Optionnel', 'esi_peppol'); ?>)</span>
                </h3>
                <p><?php echo esc_html($settings_status['email']['description']); ?></p>
                <?php if ($settings_status['email']['completed']) : ?>
                    <div class="step-value">
                        <strong><?php esc_html_e('Email configuré :', 'esi_peppol'); ?></strong>
                        <code><?php echo esc_html($settings_status['email']['value']); ?></code>
                    </div>
                <?php else : ?>
                    <a href="<?php echo esc_url($settings_status['email']['action_url']); ?>" class="button button-secondary">
                        <?php echo esc_html($settings_status['email']['action_label']); ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Actions rapides -->
    <div class="esi-peppol-dashboard-actions">
        <h2><?php esc_html_e('Actions rapides', 'esi_peppol'); ?></h2>
        <p>
            <a href="<?php echo esc_url($settings_url); ?>" class="button button-primary button-hero">
                <?php esc_html_e('Configuration avancée', 'esi_peppol'); ?>
            </a>
            <a href="<?php echo esc_url($logs_url); ?>" class="button button-secondary button-hero" style="margin-left: 10px;">
                <?php esc_html_e('Journal des envois Peppol', 'esi_peppol'); ?>
            </a>
        </p>
    </div>
</div>
