<?php
/**
 * SEO Analytics Admin Dashboard
 * Secure admin panel for viewing and managing SEO analytics
 */

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// Check admin authorization (compatible with existing admin system)
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
    header('Location: ../admin_login.php?redirect=admin/seo_analytics/dashboard.php');
    exit;
}

// Load configuration
$config = include __DIR__ . '/../../helpers/seo_analytics/config.php';

// Load API classes if they exist
$gsc_available = file_exists(__DIR__ . '/../../helpers/seo_analytics/GoogleSearchConsoleAPI.php');
$ga4_available = file_exists(__DIR__ . '/../../helpers/seo_analytics/GoogleAnalytics4API.php');
$backlink_available = file_exists(__DIR__ . '/../../helpers/seo_analytics/BacklinkAnalyzerAPI.php');
$audit_available = file_exists(__DIR__ . '/../../helpers/seo_analytics/SiteAuditAPI.php');

if ($gsc_available) require_once __DIR__ . '/../../helpers/seo_analytics/GoogleSearchConsoleAPI.php';
if ($ga4_available) require_once __DIR__ . '/../../helpers/seo_analytics/GoogleAnalytics4API.php';
if ($backlink_available) require_once __DIR__ . '/../../helpers/seo_analytics/BacklinkAnalyzerAPI.php';
if ($audit_available) require_once __DIR__ . '/../../helpers/seo_analytics/SiteAuditAPI.php';

$page_title = 'SEO Analytics Dashboard';
$active_section = $_GET['section'] ?? 'overview';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?> - Admin Dashboard</title>
    <link rel="stylesheet" href="../../assets/seo_analytics/dashboard.css">
    <script src="https://cdn.jsdelivr.net/npm/apexcharts@latest"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js@latest"></script>
</head>
<body class="admin-panel">
    <div class="admin-container">
        <!-- Sidebar Navigation -->
        <aside class="admin-sidebar">
            <div class="sidebar-header">
                <h2>SEO Analytics</h2>
                <p>Dashboard</p>
            </div>
            <nav class="sidebar-nav">
                <a href="?section=overview" class="nav-item <?php echo $active_section === 'overview' ? 'active' : ''; ?>">
                    <span class="icon">📊</span> Overview
                </a>
                <a href="?section=search_performance" class="nav-item <?php echo $active_section === 'search_performance' ? 'active' : ''; ?>">
                    <span class="icon">🔍</span> Search Performance
                </a>
                <a href="?section=realtime" class="nav-item <?php echo $active_section === 'realtime' ? 'active' : ''; ?>">
                    <span class="icon">⚡</span> Realtime Activity
                </a>
                <a href="?section=backlinks" class="nav-item <?php echo $active_section === 'backlinks' ? 'active' : ''; ?>">
                    <span class="icon">🔗</span> Backlinks
                </a>
                <a href="?section=site_audit" class="nav-item <?php echo $active_section === 'site_audit' ? 'active' : ''; ?>">
                    <span class="icon">🔧</span> Site Audit
                </a>
                <a href="?section=keywords" class="nav-item <?php echo $active_section === 'keywords' ? 'active' : ''; ?>">
                    <span class="icon">🎯</span> Keywords
                </a>
                <a href="?section=settings" class="nav-item <?php echo $active_section === 'settings' ? 'active' : ''; ?>">
                    <span class="icon">⚙️</span> Settings
                </a>
            </nav>
        </aside>

        <!-- Main Content -->
        <main class="admin-main">
            <div class="admin-header">
                <h1><?php echo $page_title; ?></h1>
                <div class="header-actions">
                    <button class="btn btn-primary" id="refresh-data">🔄 Refresh Data</button>
                    <button class="btn btn-secondary" id="export-report">📥 Export Report</button>
                </div>
            </div>

            <div class="content-section">
                <?php
                // Load appropriate section
                switch ($active_section) {
                    case 'search_performance':
                        include 'sections/search_performance.php';
                        break;
                    case 'realtime':
                        include 'sections/realtime_activity.php';
                        break;
                    case 'backlinks':
                        include 'sections/backlinks.php';
                        break;
                    case 'site_audit':
                        include 'sections/site_audit.php';
                        break;
                    case 'keywords':
                        include 'sections/keywords.php';
                        break;
                    case 'settings':
                        include 'sections/settings.php';
                        break;
                    default:
                        include 'sections/overview.php';
                }
                ?>
            </div>
        </main>
    </div>

    <!-- Scripts -->
    <script src="../../assets/seo_analytics/dashboard.js"></script>
</body>
</html>
