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

$api_key = Launchmind_Blog_Connector::get_option('api_key', '');
$is_configured = !empty($api_key);
$current_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'articles';

$posts = array();
$posts_error = null;

if ($is_configured) {
    $posts = Launchmind_API::get_posts(array('limit' => 50));
    if (is_wp_error($posts)) {
        $posts_error = $posts->get_error_message();
        $posts = array();
    }
}

$week_ago = strtotime('-7 days');
$month_ago = strtotime('-30 days');
$this_week = 0;
$this_month = 0;
foreach ($posts as $post) {
    $published = !empty($post['publishedAt']) ? strtotime($post['publishedAt']) : 0;
    if ($published > $week_ago) {
        $this_week++;
    }
    if ($published > $month_ago) {
        $this_month++;
    }
}
?>

<div class="launchmind-dashboard">
    <div class="lm-header">
        <div class="lm-header__info">
            <div class="lm-logo">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="white" aria-hidden="true">
                    <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5" />
                </svg>
            </div>
            <div class="lm-header__text">
                <h1><?php esc_html_e('Launchmind Blog', 'launchmind-blog'); ?></h1>
                <p><?php esc_html_e('AI-powered blog content for your WordPress site', 'launchmind-blog'); ?></p>
            </div>
        </div>
        <?php if ($is_configured): ?>
        <div class="lm-header__actions">
            <button type="button" class="button" id="lm-refresh-btn">
                <span class="dashicons dashicons-update"></span>
                <?php esc_html_e('Refresh', 'launchmind-blog'); ?>
            </button>
            <a class="button button-primary" target="_blank" rel="noopener noreferrer" href="https://launchmind.io/backlinks/dashboard?utm_source=wordpress&utm_medium=plugin">
                <span class="dashicons dashicons-edit"></span>
                <?php esc_html_e('Manage Content', 'launchmind-blog'); ?>
            </a>
        </div>
        <?php endif; ?>
    </div>

    <?php if ($is_configured): ?>
    <div class="lm-stats">
        <div class="lm-stat-card">
            <div class="lm-stat-value"><?php echo (int) count($posts); ?></div>
            <div class="lm-stat-label"><?php esc_html_e('Total Articles', 'launchmind-blog'); ?></div>
        </div>
        <div class="lm-stat-card">
            <div class="lm-stat-value"><?php echo (int) $this_week; ?></div>
            <div class="lm-stat-label"><?php esc_html_e('This Week', 'launchmind-blog'); ?></div>
        </div>
        <div class="lm-stat-card">
            <div class="lm-stat-value"><?php echo (int) $this_month; ?></div>
            <div class="lm-stat-label"><?php esc_html_e('This Month', 'launchmind-blog'); ?></div>
        </div>
        <div class="lm-stat-card lm-stat-card--status">
            <div class="lm-stat-status">
                <span class="lm-status-dot lm-status-dot--active"></span>
                <?php esc_html_e('Active', 'launchmind-blog'); ?>
            </div>
            <div class="lm-stat-label"><?php esc_html_e('Connection', 'launchmind-blog'); ?></div>
        </div>
    </div>

    <div class="lm-notice lm-notice--info">
        <span class="dashicons dashicons-edit"></span>
        <p>
            <strong><?php esc_html_e('Want to edit, create, or delete articles?', 'launchmind-blog'); ?></strong>
            <?php esc_html_e('All content management is done in your', 'launchmind-blog'); ?>
            <a href="https://launchmind.io/backlinks/dashboard?utm_source=wordpress&utm_medium=plugin" target="_blank" rel="noopener noreferrer">
                <?php esc_html_e('Launchmind Dashboard', 'launchmind-blog'); ?>
            </a>.
            <?php esc_html_e('Changes will automatically sync to your site.', 'launchmind-blog'); ?>
        </p>
    </div>
    <?php endif; ?>

    <?php if ($posts_error): ?>
        <div class="notice notice-error"><p><?php echo esc_html($posts_error); ?></p></div>
    <?php endif; ?>

    <div class="lm-tabs">
        <nav class="lm-tabs__nav">
            <a href="?page=launchmind-dashboard&tab=articles" class="lm-tab <?php echo esc_attr( $current_tab === 'articles' ? 'lm-tab--active' : '' ); ?>">
                <?php esc_html_e('Articles', 'launchmind-blog'); ?>
                <span class="lm-tab__badge"><?php echo (int) count($posts); ?></span>
            </a>
            <a href="?page=launchmind-dashboard&tab=analytics" class="lm-tab <?php echo esc_attr( $current_tab === 'analytics' ? 'lm-tab--active' : '' ); ?>">
                <?php esc_html_e('Analytics', 'launchmind-blog'); ?>
            </a>
            <a href="?page=launchmind-dashboard&tab=settings" class="lm-tab <?php echo esc_attr( $current_tab === 'settings' ? 'lm-tab--active' : '' ); ?>">
                <?php esc_html_e('Settings', 'launchmind-blog'); ?>
            </a>
        </nav>

        <div class="lm-tabs__content">
            <?php
            switch ($current_tab) {
                case 'analytics':
                    include LAUNCHMIND_BLOG_PLUGIN_DIR . 'admin/views/tab-analytics.php';
                    break;
                case 'settings':
                    include LAUNCHMIND_BLOG_PLUGIN_DIR . 'admin/views/tab-settings.php';
                    break;
                case 'articles':
                default:
                    include LAUNCHMIND_BLOG_PLUGIN_DIR . 'admin/views/tab-articles.php';
                    break;
            }
            ?>
        </div>
    </div>
</div>

<div class="lm-modal" id="lm-post-modal" style="display:none;">
    <div class="lm-modal__overlay"></div>
    <div class="lm-modal__content">
        <div class="lm-modal__header">
            <h2 class="lm-modal__title"></h2>
            <button type="button" class="lm-modal__close" aria-label="Close">&times;</button>
        </div>
        <div class="lm-modal__body"></div>
        <div class="lm-modal__footer">
            <a href="https://launchmind.io/backlinks/dashboard" target="_blank" rel="noopener noreferrer" class="button button-primary">
                <?php esc_html_e('Edit in Launchmind', 'launchmind-blog'); ?>
            </a>
            <button type="button" class="button lm-modal__close-btn"><?php esc_html_e('Close', 'launchmind-blog'); ?></button>
        </div>
    </div>
</div>
