<?php
/**
 * Admin Dashboard Display
 */

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

$generator = new CCSPLLMS_Generator();
$analytics = new CCSPLLMS_Analytics();
$cron = new CCSPLLMS_Cron();

$last_generated = get_option('ccspllms_last_generated');
$file_stats = get_option('ccspllms_file_stats', array());
$analytics_data = $analytics->get_analytics_data();
$next_scheduled = $cron->get_next_scheduled();
?>

<div class="ccspllms_container">
    <div class="ccspllms_header">
        <div class="ccspllmshead_left">
            <div class="ccspllmshead_logo">
                <img src="<?php echo esc_url( CCSPLLMS_PLUGIN_URL . 'assets/cloudcusplogo.svg' ); ?>" alt="<?php echo esc_attr__( 'Logo', 'cloudcusp-llms-txt-generator' ); ?>" class="ccspllms_logo">

            </div>
            <div class="ccspllmshead_text"><h1>LLMS.txt  Generator</h1></div>
        </div>
        <div class="ccspllmshead_right">
            <h1><i class="fas fa-robot"></i> Dashboard</h1>
            <p>Manage your AI-friendly content file</p>
        </div>
        
    </div>

    <nav class="ccspllms_nav">
        <ul class="ccspllms_nav_list">
            <li><a href="#dashboard" class="ccspllms_nav_link active" data-tab="dashboard">Dashboard</a></li>
            <li><a href="#generator" class="ccspllms_nav_link" data-tab="generator">Generator</a></li>
            <li><a href="#preview" class="ccspllms_nav_link" data-tab="preview">Preview</a></li>
            <li><a href="#logs" class="ccspllms_nav_link" data-tab="logs">Logs</a></li>
        </ul>
    </nav>

    <div class="ccspllms_content">
        <!-- Dashboard Tab -->
        <div id="ccspllms_dashboard" class="ccspllms_tab_content active">
            <div class="ccspllms_stats_grid">
                <div class="ccspllms_stat_card">
                    <h3>Total Posts</h3>
                    <div class="stat-number">
                        <?php echo esc_html( $file_stats['total'] ?? 0 ); ?>
                    </div>

                </div>
                <div class="ccspllms_stat_card">
                    <h3>Bot Visits Today</h3>
                    <div class="stat-number"><?php echo esc_html( $analytics_data['totals']['today_visits'] ?? 0 ); ?></div>
                </div>
                <div class="ccspllms_stat_card">
                    <h3>File Size</h3>
                    <div class="stat-number ccspllms_file_size"><?php echo esc_html( $generator->get_file_size() ); ?></div>
                </div>
                <div class="ccspllms_stat_card">
                    <h3>Last Generated</h3>
                    <div class="stat-number ccspllms_last_generated">
                        <?php
                        if ( $last_generated ) {
                            echo esc_html( gmdate( 'M j, Y H:i', strtotime( $last_generated ) ) );
                        } else {
                            echo esc_html__( 'Never', 'cloudcusp-llms-txt-generator' );
                        }
                        ?>
                    </div>
                </div>
            </div>

            <div class="ccspllms_card">
                <h3>Quick Actions</h3>
                <button id="ccspllms_regenerate_btn" class="ccspllms_button">
                    <i class="fas fa-sync-alt"></i> Regenerate File
                </button>
                <a href="<?php echo esc_url( get_site_url() ); ?>/llms.txt" target="_blank" class="ccspllms_button">
                    <i class="fas fa-external-link-alt"></i> View File
                </a>
                <button id="ccspllms_download_btn" class="ccspllms_button">
                    <i class="fas fa-download"></i> Download File
                </button>
            </div>

            <div class="ccspllms_card">
                <h3>System Status</h3>
                <table class="ccspllms_table">
                    <tr>
                        <td>Plugin Status</td>
                        <td><span class="status-active">Active</span></td>
                    </tr>
                    <tr>
                        <td>File Generation</td>
                        <td><?php echo  get_option('ccspllms_enabled') ? '<span class="status-active">Enabled</span>' : '<span class="status-inactive">Disabled</span>' ; ?></td>
                    </tr>
                    <tr>
                        <td>Auto Regeneration</td>
                        <td><?php echo get_option('ccspllms_auto_regenerate') ? '<span class="status-active">Enabled</span>' : '<span class="status-inactive">Disabled</span>' ; ?></td>
                    </tr>
                    <tr>
                        <td><?php echo esc_html__( 'Next Scheduled', 'cloudcusp-llms-txt-generator' ); ?></td>
                        <td>
                            <?php
                            if ( $next_scheduled ) {
                                echo esc_html( gmdate( 'M j, Y H:i', strtotime( $next_scheduled ) ) );
                            } else {
                                echo esc_html__( 'Not scheduled', 'cloudcusp-llms-txt-generator' );
                            }
                            ?>
                        </td>
                    </tr>

                </table>
            </div>
        </div>

        <!-- Generator Tab -->
        <div id="ccspllms_generator" class="ccspllms_tab_content">
            <!-- <div class="ccspllms_card">
                <h3>File Generation</h3>
                <p>Generate or regenerate your LLMS.txt file with current content.</p>
                
                <div class="generation-options">
                    <label>
                        <input type="checkbox" id="include_drafts" /> Include draft posts
                    </label>
                    <label>
                        <input type="checkbox" id="force_regenerate" checked /> Force regeneration
                    </label>
                </div>
                
                <button id="ccspllms_generate_now" class="ccspllms_button">
                    <i class="fas fa-cog"></i> Generate Now
                </button>
            </div> -->

            <div class="ccspllms_card">
                <h3>Generation History</h3>
                <div id="generation_history">
                    <?php
                    $history = $cron->get_generation_history(10);
                    if ( $history ) {
                        echo '<table class="ccspllms_table">';
                        echo '<thead><tr><th>' . esc_html__( 'Date', 'cloudcusp-llms-txt-generator' ) . '</th><th>' . esc_html__( 'Trigger', 'cloudcusp-llms-txt-generator' ) . '</th><th>' . esc_html__( 'Status', 'cloudcusp-llms-txt-generator' ) . '</th><th>' . esc_html__( 'Posts', 'cloudcusp-llms-txt-generator' ) . '</th></tr></thead>';
                        echo '<tbody>';

                        foreach ( $history as $entry ) {
                            $status   = $entry->success ? '<span class="status-success">Success</span>' : '<span class="status-error">Failed</span>';
                            $datetime = $entry->generated_at ? gmdate( 'M j, Y H:i', strtotime( $entry->generated_at ) ) : '';

                            echo '<tr>';
                            echo '<td>' . esc_html( $datetime ) . '</td>';
                            echo '<td>' . esc_html( ucfirst( $entry->trigger_type ) ) . '</td>';
                            echo '<td>' . wp_kses_post( $status ) . '</td>';
                            echo '<td>' . esc_html( $entry->posts_count ?? __( 'N/A', 'cloudcusp-llms-txt-generator' ) ) . '</td>';
                            echo '</tr>';
                        }

                        echo '</tbody></table>';
                    } else {
                        echo '<p>No generation history available.</p>';
                    }
                    ?>
                </div>
            </div>
        </div>

        <!-- Preview Tab -->
        <div id="ccspllms_preview" class="ccspllms_tab_content">
            <div class="ccspllms_card">
                <h3>File Preview</h3>
                <div class="file-preview">
                    <textarea readonly style="width: 100%; height: 400px; font-family: monospace;">
<?php
$content = $generator->get_cached_content();
echo esc_textarea($content ?: 'No content generated yet. Click "Generate Now" to create the file.');
?>
                    </textarea>
                </div>
                <p class="description">This is a preview of your current LLMS.txt file content.</p>
            </div>
        </div>

        <!-- Logs Tab -->
        <div id="ccspllms_logs" class="ccspllms_tab_content">
            <div class="ccspllms_card">
                <h3>Recent Bot Activity</h3>
                <?php if (!empty($analytics_data['recent_visits'])): ?>
                    <table class="ccspllms_table">
                        <thead>
                            <tr>
                                <th>Bot Name</th>
                                <th>IP Address</th>
                                <th>Time</th>
                                <th>User Agent</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($analytics_data['recent_visits'] as $visit): ?>
                                <tr>
                                    <td><?php echo esc_html($visit->bot_name ?: 'Unknown'); ?></td>
                                    <td><?php echo esc_html($visit->ip_address); ?></td>
                                    <td><?php echo esc_html(gmdate('M j, H:i', strtotime($visit->request_time))); ?></td>
                                    <td title="<?php echo esc_attr($visit->user_agent); ?>">
                                        <?php echo esc_html(substr($visit->user_agent, 0, 50)) . '...'; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php else: ?>
                    <p>No bot visits recorded yet.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>