<?php
/**
 * Dashboard Page
 *
 * @package AIContentGenerator
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Include License Manager
if ( ! class_exists( 'AICG\License_Manager' ) ) {
    require_once ROIDAICO_PLUGIN_DIR . 'includes/class-license-manager.php';
}

$is_pro = \AICG\License_Manager::is_pro();
$upgrade_url = \AICG\License_Manager::get_upgrade_url();

$keyword_manager = new AICG\Keyword_Manager();
$stats = $keyword_manager->get_stats();
?>

<div class="wrap roidaico-wrap">
    <h1>
        <?php esc_html_e('AI Content Generator', 'roidnet-ai-content-generator'); ?>
    </h1>

    <?php if ( ! $is_pro ) : ?>
    <div class="notice notice-info" style="display: flex; align-items: center; justify-content: space-between; padding: 20px; margin: 20px 0 0 0; background: #fff; border-left: 4px solid #007cba; box-shadow: 0 1px 1px rgba(0,0,0,.04);">
        <div style="display: flex; align-items: center; gap: 15px;">
            <span class="dashicons dashicons-admin-plugins" style="font-size: 32px; width: 32px; height: 32px; color: #007cba;"></span>
            <div>
                <h3 style="margin: 0 0 5px 0; font-size: 16px; font-weight: 600; color: #1d2327;">
                    <?php esc_html_e('Upgrade to Pro - Start Your 7-Day Free Trial!', 'roidnet-ai-content-generator'); ?>
                </h3>
                <p style="margin: 0; color: #646970; font-size: 14px;">
                    <?php esc_html_e('Unlock premium features: AI Image Generation, SEO integrations, unlimited keywords queue, and automated scheduling.', 'roidnet-ai-content-generator'); ?>
                </p>
            </div>
        </div>
        <div>
            <a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary button-hero" style="font-size: 16px; padding: 8px 24px; line-height: normal; height: auto;">
                <?php esc_html_e('Start Free Trial', 'roidnet-ai-content-generator'); ?>
            </a>
        </div>
    </div>
    <?php endif; ?>

    <div class="roidaico-dashboard">
        <!-- Stats Cards -->
        <div class="roidaico-stats-grid">
            <div class="roidaico-stat-card">
                <div class="roidaico-stat-icon dashicons dashicons-edit"></div>
                <div class="roidaico-stat-content">
                    <span class="roidaico-stat-number">
                        <?php echo esc_html($stats->pending ?? 0); ?>
                    </span>
                    <span class="roidaico-stat-label">
                        <?php esc_html_e('Pending Keywords', 'roidnet-ai-content-generator'); ?>
                    </span>
                </div>
            </div>

            <div class="roidaico-stat-card">
                <div class="roidaico-stat-icon dashicons dashicons-clock"></div>
                <div class="roidaico-stat-content">
                    <span class="roidaico-stat-number">
                        <?php echo esc_html($stats->processing ?? 0); ?>
                    </span>
                    <span class="roidaico-stat-label">
                        <?php esc_html_e('Processing', 'roidnet-ai-content-generator'); ?>
                    </span>
                </div>
            </div>

            <div class="roidaico-stat-card">
                <div class="roidaico-stat-icon dashicons dashicons-yes-alt"></div>
                <div class="roidaico-stat-content">
                    <span class="roidaico-stat-number">
                        <?php echo esc_html($stats->completed ?? 0); ?>
                    </span>
                    <span class="roidaico-stat-label">
                        <?php esc_html_e('Completed', 'roidnet-ai-content-generator'); ?>
                    </span>
                </div>
            </div>

            <div class="roidaico-stat-card">
                <div class="roidaico-stat-icon dashicons dashicons-warning"></div>
                <div class="roidaico-stat-content">
                    <span class="roidaico-stat-number">
                        <?php echo esc_html($stats->failed ?? 0); ?>
                    </span>
                    <span class="roidaico-stat-label">
                        <?php esc_html_e('Failed', 'roidnet-ai-content-generator'); ?>
                    </span>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="roidaico-section">
            <h2>
                <?php esc_html_e('Quick Actions', 'roidnet-ai-content-generator'); ?>
            </h2>
            <div class="roidaico-quick-actions">
                <a href="<?php echo esc_url(admin_url('admin.php?page=roidaico-keywords')); ?>"
                    class="button button-primary button-hero">
                    <span class="dashicons dashicons-plus-alt"></span>
                    <?php esc_html_e('Add Keywords', 'roidnet-ai-content-generator'); ?>
                </a>
                <a href="<?php echo esc_url(admin_url('admin.php?page=roidaico-schedules')); ?>"
                    class="button button-hero">
                    <span class="dashicons dashicons-calendar-alt"></span>
                    <?php esc_html_e('Manage Schedules', 'roidnet-ai-content-generator'); ?>
                </a>
                <a href="<?php echo esc_url(admin_url('admin.php?page=roidaico-settings')); ?>"
                    class="button button-hero">
                    <span class="dashicons dashicons-admin-generic"></span>
                    <?php esc_html_e('Settings', 'roidnet-ai-content-generator'); ?>
                </a>
            </div>
        </div>

        <!-- API Status -->
        <div class="roidaico-section">
            <h2>
                <?php esc_html_e('API Status', 'roidnet-ai-content-generator'); ?>
            </h2>
            <table class="roidaico-status-table">
                <?php 
                $provider = get_option('roidaico_ai_provider', 'openrouter');
                if ($provider === 'wp_ai_client') : 
                ?>
                <tr>
                    <td>
                        <?php esc_html_e('WordPress AI Client', 'roidnet-ai-content-generator'); ?>
                    </td>
                    <td>
                        <?php if (function_exists('wp_ai_client_prompt')): ?>
                            <span class="roidaico-status-badge roidaico-status-configured">
                                <?php esc_html_e('Active (WordPress Native)', 'roidnet-ai-content-generator'); ?>
                            </span>
                        <?php else: ?>
                            <span class="roidaico-status-badge roidaico-status-not-configured">
                                <?php esc_html_e('Unsupported', 'roidnet-ai-content-generator'); ?>
                            </span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php else : ?>
                <tr>
                    <td>
                        <?php esc_html_e('OpenRouter API', 'roidnet-ai-content-generator'); ?>
                    </td>
                    <td>
                        <?php if (get_option('roidaico_openrouter_api_key')): ?>
                            <span class="roidaico-status-badge roidaico-status-configured">
                                <?php esc_html_e('Configured', 'roidnet-ai-content-generator'); ?>
                            </span>
                        <?php else: ?>
                            <span class="roidaico-status-badge roidaico-status-not-configured">
                                <?php esc_html_e('Not Configured', 'roidnet-ai-content-generator'); ?>
                            </span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td>
                        <?php esc_html_e('Pexels API', 'roidnet-ai-content-generator'); ?>
                    </td>
                    <td>
                        <?php if (get_option('roidaico_pexels_api_key')): ?>
                            <span class="roidaico-status-badge roidaico-status-configured">
                                <?php esc_html_e('Configured', 'roidnet-ai-content-generator'); ?>
                            </span>
                        <?php else: ?>
                            <span class="roidaico-status-badge roidaico-status-not-configured">
                                <?php esc_html_e('Not Configured', 'roidnet-ai-content-generator'); ?>
                            </span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td>
                        <?php esc_html_e('Selected Model', 'roidnet-ai-content-generator'); ?>
                    </td>
                    <td>
                        <code><?php echo esc_html($provider === 'wp_ai_client' ? __('Managed by WordPress', 'roidnet-ai-content-generator') : get_option('roidaico_openrouter_model', 'openai/gpt-4o-mini')); ?></code>
                    </td>
                </tr>
            </table>
        </div>

        <!-- Recent Activity -->
        <div class="roidaico-section">
            <h2>
                <?php esc_html_e('Recent Generated Posts', 'roidnet-ai-content-generator'); ?>
            </h2>
            <?php
            $recent_posts = get_posts(array(
                'post_type' => 'post',
                'post_status' => array('publish', 'draft'),
                'meta_key' => '_roidaico_generated',
                'meta_value' => '1',
                'numberposts' => 5,
                'orderby' => 'date',
                'order' => 'DESC',
            ));

            if ($recent_posts):
                ?>
                <table class="wp-list-table widefat fixed striped">
                    <thead>
                        <tr>
                            <th>
                                <?php esc_html_e('Title', 'roidnet-ai-content-generator'); ?>
                            </th>
                            <th>
                                <?php esc_html_e('Status', 'roidnet-ai-content-generator'); ?>
                            </th>
                            <th>
                                <?php esc_html_e('Date', 'roidnet-ai-content-generator'); ?>
                            </th>
                            <th>
                                <?php esc_html_e('Actions', 'roidnet-ai-content-generator'); ?>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent_posts as $post): ?>
                            <tr>
                                <td>
                                    <strong>
                                        <?php echo esc_html($post->post_title); ?>
                                    </strong>
                                    <br>
                                    <small>
                                        <?php echo esc_html(get_post_meta($post->ID, '_roidaico_keyword', true)); ?>
                                    </small>
                                </td>
                                <td>
                                    <span class="roidaico-post-status roidaico-post-<?php echo esc_attr($post->post_status); ?>">
                                        <?php echo esc_html(ucfirst($post->post_status)); ?>
                                    </span>
                                </td>
                                <td>
                                    <?php echo esc_html(get_the_date('', $post)); ?>
                                </td>
                                <td>
                                    <a href="<?php echo esc_url(get_edit_post_link($post->ID)); ?>"
                                        class="button button-small">
                                        <?php esc_html_e('Edit', 'roidnet-ai-content-generator'); ?>
                                    </a>
                                    <?php if ($post->post_status === 'publish'): ?>
                                        <a href="<?php echo esc_url(get_permalink($post->ID)); ?>" class="button button-small"
                                            target="_blank">
                                            <?php esc_html_e('View', 'roidnet-ai-content-generator'); ?>
                                        </a>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <p class="description">
                    <?php esc_html_e('No posts generated yet. Add keywords to get started!', 'roidnet-ai-content-generator'); ?>
                </p>
            <?php endif; ?>
        </div>
    </div>
</div>