<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

$drafts = get_option( 'mt_primelink_import_drafts', array() );
$drafts = is_array( $drafts ) ? $drafts : array();

$total_imports = count( $drafts );
$running = $paused = $finished = 0;
$recent = array();

foreach ( $drafts as $id => $draft ) {
    $state  = get_option( 'mt_primelink_import_state_' . absint( $id ), array() );
    $status = $state['status'] ?? 'idle';

    if ( $status === 'running' ) {
        $running++;
    } elseif ( $status === 'paused' ) {
        $paused++;
    } elseif ( $status === 'finished' ) {
        $finished++;
    }

    $recent[] = array(
        'id'        => $id,
        'created'   => $draft['created_at'] ?? '',
        'step'      => $draft['current_step'] ?? 1,
        'status'    => $status,
        'current'   => $state['current'] ?? 0,
        'total'     => $state['total'] ?? 0,
        'progress'  => $state['progress'] ?? 0,
    );
}

usort( $recent, function( $a, $b ) {
    $at = strtotime( $a['created'] ?? '' );
    $bt = strtotime( $b['created'] ?? '' );
    return $bt <=> $at;
});
$recent = array_slice( $recent, 0, 5 );
$last_status        = 'idle';
if ( ! empty( $recent[0]['status'] ) ) {
    $last_status = $recent[0]['status'];
}
$release_notes_url  = 'https://primelink.malotop.ro/release-notes';
$wizard_completed   = get_option( 'mt_primelink_wizard_completed', false );

// Prepare global header context.
ob_start();
?>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mt-primelink-new-import' ) ); ?>" class="button button-primary">
    <?php esc_html_e( 'New Import', 'primelink' ); ?>
</a>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=mt-primelink-manage-import' ) ); ?>" class="button">
    <?php esc_html_e( 'Manage Imports', 'primelink' ); ?>
</a>
<button type="button" class="button button-primary" id="mt-primelink-restart-wizard" title="<?php echo $wizard_completed ? esc_attr__( 'Restart Setup Wizard', 'primelink' ) : esc_attr__( 'Start Setup Wizard', 'primelink' ); ?>">
    <span class="dashicons dashicons-update"></span>
    <?php esc_html_e( 'Setup Wizard', 'primelink' ); ?>
</button>
<button type="button" class="button mt_primelink-shortcuts-info" id="mt-primelink-show-shortcuts" title="<?php esc_attr_e( 'Keyboard Shortcuts', 'primelink' ); ?>">
    <span class="dashicons dashicons-info"></span>
</button>
<?php
$header_actions       = ob_get_clean();
$header_eyebrow       = __( 'Command Center', 'primelink' );
$header_title         = __( 'PrimeLink Dashboard', 'primelink' );
$header_description   = __( 'Monitor imports, resume runs, and jump back into the wizard.', 'primelink' );
$header_status_label  = ucfirst( $last_status );
$header_status_class  = 'mt_primelink-status-' . $last_status;
$header_release_url   = $release_notes_url;
$header_release_label = sprintf(
    /* translators: %s: plugin version */
    __( 'Release notes · %s', 'primelink' ),
    MT_PRIMELINK_VERSION
);
?>
<div class="wrap mt-primelink-wrap mt_primelink-dashboard">
    <?php
    // Global admin header.
    include MT_PRIMELINK_PATH . 'templates/admin/partials/header.php';
    ?>

    <div class="mt_primelink-dashboard__grid">
        <div class="mt_primelink-dash-card glass">
            <div class="mt_primelink-dash-label"><?php esc_html_e( 'Total Imports', 'primelink' ); ?></div>
            <div class="mt_primelink-dash-value"><?php echo (int) $total_imports; ?></div>
        </div>
        <div class="mt_primelink-dash-card glass">
            <div class="mt_primelink-dash-label"><?php esc_html_e( 'Running', 'primelink' ); ?></div>
            <div class="mt_primelink-dash-value is-running"><?php echo (int) $running; ?></div>
        </div>
        <div class="mt_primelink-dash-card glass">
            <div class="mt_primelink-dash-label"><?php esc_html_e( 'Paused', 'primelink' ); ?></div>
            <div class="mt_primelink-dash-value is-paused"><?php echo (int) $paused; ?></div>
        </div>
        <div class="mt_primelink-dash-card glass">
            <div class="mt_primelink-dash-label"><?php esc_html_e( 'Finished', 'primelink' ); ?></div>
            <div class="mt_primelink-dash-value is-finished"><?php echo (int) $finished; ?></div>
        </div>
    </div>

    <div class="mt_primelink-dash-panel glass">
        <div class="mt_primelink-dash-panel__header">
            <div>
                <div class="mt_primelink-summary-eyebrow"><?php esc_html_e( 'Recent Imports', 'primelink' ); ?></div>
                <h3 style="margin:4px 0 0;"><?php esc_html_e( 'Latest activity', 'primelink' ); ?></h3>
            </div>
            <a href="<?php echo esc_url( admin_url( 'admin.php?page=mt-primelink-manage-import' ) ); ?>" class="button">
                <?php esc_html_e( 'View all', 'primelink' ); ?>
            </a>
        </div>

        <?php if ( empty( $recent ) ) : ?>
            <div class="mt_primelink-empty"><?php esc_html_e( 'No imports yet. Start your first import to see activity here.', 'primelink' ); ?></div>
        <?php else : ?>
            <div class="mt_primelink-table-wrapper">
            <table class="mt_primelink-dash-table">
                <thead>
                    <tr>
                        <th><?php esc_html_e( 'Import ID', 'primelink' ); ?></th>
                        <th><?php esc_html_e( 'Created', 'primelink' ); ?></th>
                        <th><?php esc_html_e( 'Step', 'primelink' ); ?></th>
                        <th><?php esc_html_e( 'Status', 'primelink' ); ?></th>
                        <th><?php esc_html_e( 'Progress', 'primelink' ); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ( $recent as $row ) : ?>
                        <?php
                            $progress = isset( $row['progress'] ) ? (int) $row['progress'] : 0;
                            $curr     = isset( $row['current'] ) ? (int) $row['current'] : 0;
                            $total    = isset( $row['total'] ) ? (int) $row['total'] : 0;
                        ?>
                        <tr>
                            <td>#<?php echo (int) $row['id']; ?></td>
                            <td><?php echo $row['created'] ? esc_html( $row['created'] ) : '&mdash;'; ?></td>
                            <td><?php echo (int) $row['step']; ?></td>
                            <td><span class="mt_primelink-status-pill mt_primelink-status-<?php echo esc_attr( $row['status'] ); ?>"><?php echo esc_html( ucfirst( $row['status'] ) ); ?></span></td>
                            <td>
                                <div class="mt_primelink-dash-progress">
                                    <div class="mt_primelink-dash-progress__bar" style="width:<?php echo max( 0, min( 100, $progress ) ); ?>%;"></div>
                                </div>
                                <small class="muted"><?php echo esc_html( $curr . ' / ' . $total ); ?></small>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            </div>
        <?php endif; ?>
    </div>
</div>
