<?php
/**
 * Import view: detect competitor SEO plugins and offer one-click migration.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$adapters = Seoforge_Migrate::adapters_available();
$state    = Seoforge_Migrate::get_state();
?>
<div class="seoforge-card">
    <h2><?php esc_html_e( 'Import from your existing SEO plugin', 'brainwerk-seo-suite' ); ?></h2>
    <p class="description">
        <?php esc_html_e( 'Brainwerk SEO Suite can pull your titles, meta descriptions, focus keywords, Open Graph tags, canonicals and redirects out of Yoast, Rank Math or All in One SEO. The original plugin is not touched — you can keep both active during the switch and disable the old one once you\'re happy.', 'brainwerk-seo-suite' ); ?>
    </p>
</div>

<?php if ( empty( $adapters ) ) : ?>
    <div class="seoforge-card">
        <p><?php esc_html_e( 'No supported SEO plugin detected. Yoast, Rank Math and All in One SEO are supported.', 'brainwerk-seo-suite' ); ?></p>
    </div>
<?php else : ?>
    <div class="seoforge-card-grid">
        <?php foreach ( $adapters as $a ) : ?>
            <div class="seoforge-card seoforge-import-card">
                <h3><?php echo esc_html( $a['label'] ); ?>
                    <?php if ( $state['source'] === $a['id'] && 'done' === $state['status'] ) : ?>
                        <span class="seoforge-pill seoforge-pill-good"><?php esc_html_e( 'imported', 'brainwerk-seo-suite' ); ?></span>
                    <?php endif; ?>
                </h3>
                <p>
                    <strong><?php echo esc_html( number_format_i18n( $a['posts'] ) ); ?></strong>
                    <?php esc_html_e( 'posts with SEO meta', 'brainwerk-seo-suite' ); ?><br>
                    <strong><?php echo esc_html( number_format_i18n( $a['redirects'] ) ); ?></strong>
                    <?php esc_html_e( 'redirects', 'brainwerk-seo-suite' ); ?>
                </p>
                <p>
                    <button type="button" class="button button-primary sf-import-start"
                            data-source="<?php echo esc_attr( $a['id'] ); ?>"
                            <?php disabled( 'running' === $state['status'] ); ?>>
                        <?php
                        printf(
                            /* translators: %s plugin name */
                            esc_html__( 'Import from %s', 'brainwerk-seo-suite' ),
                            esc_html( $a['label'] )
                        );
                        ?>
                    </button>
                </p>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

<div class="seoforge-card" id="sf-import-progress" style="<?php echo 'idle' === $state['status'] ? 'display:none' : ''; ?>">
    <h3><?php esc_html_e( 'Migration progress', 'brainwerk-seo-suite' ); ?></h3>
    <?php $pct = $state['total_posts'] > 0 ? min( 100, (int) round( $state['done_posts'] / $state['total_posts'] * 100 ) ) : 0; ?>
    <div class="seoforge-progress">
        <span id="sf-progress-bar" style="width:<?php echo (int) $pct; ?>%;"></span>
    </div>
    <p>
        <strong><span id="sf-progress-pct"><?php echo (int) $pct; ?></span>%</strong>
        <?php esc_html_e( 'complete', 'brainwerk-seo-suite' ); ?> —
        <span id="sf-progress-done"><?php echo (int) $state['done_posts']; ?></span>
        / <span id="sf-progress-total"><?php echo (int) $state['total_posts']; ?></span>
        <?php esc_html_e( 'posts', 'brainwerk-seo-suite' ); ?>,
        <span id="sf-progress-meta"><?php echo (int) $state['imported_meta']; ?></span> <?php esc_html_e( 'meta', 'brainwerk-seo-suite' ); ?>,
        <span id="sf-progress-red"><?php echo (int) $state['imported_red']; ?></span> <?php esc_html_e( 'redirects imported', 'brainwerk-seo-suite' ); ?>.
    </p>
    <p id="sf-progress-status">
        <?php
        switch ( $state['status'] ) {
            case 'running': esc_html_e( 'Working in the background…', 'brainwerk-seo-suite' ); break;
            case 'done':    esc_html_e( 'Done. You can deactivate the old plugin now.', 'brainwerk-seo-suite' ); break;
            case 'error':   echo esc_html( 'Error: ' . $state['last_error'] ); break;
        }
        ?>
    </p>
</div>

<div class="seoforge-card">
    <h3><?php esc_html_e( 'After import', 'brainwerk-seo-suite' ); ?></h3>
    <ol style="line-height:1.9">
        <li><?php esc_html_e( 'Spot-check a few pages — view source, look for our <!-- Brainwerk SEO Suite --> block.', 'brainwerk-seo-suite' ); ?></li>
        <li><?php esc_html_e( 'In the Meta tab, switch on "Force takeover" so Brainwerk SEO Suite takes over the meta tags.', 'brainwerk-seo-suite' ); ?></li>
        <li><?php esc_html_e( 'Deactivate the old plugin.', 'brainwerk-seo-suite' ); ?></li>
        <li><?php esc_html_e( 'Submit the new sitemap (/seoforge-sitemap.xml) to Search Console.', 'brainwerk-seo-suite' ); ?></li>
    </ol>
</div>
