<?php
session_start();
require_once '../db_connect.php';

if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header('Location: ../login');
    exit;
}

// Statistics
$total_users = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE role != 'admin'"))['c'];
$total_brokers = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE role = 'broker' AND status = 'approved'"))['c'];
$total_owners = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE role = 'user' AND status = 'approved'"))['c'];
$pending_users = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM users WHERE status = 'pending'"))['c'];

$total_lands = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM lands"))['c'];
$total_area = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(total_area) as c FROM lands WHERE area_unit = 'sqm'"))['c'];

$total_alerts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM overlap_alerts"))['c'];
$resolved_alerts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as c FROM overlap_alerts WHERE is_resolved = 1"))['c'];
$pending_alerts = $total_alerts - $resolved_alerts;

// Monthly registrations (last 6 months)
$monthly_users = mysqli_query($conn, "
    SELECT DATE_FORMAT(created_at, '%Y-%m') as month, COUNT(*) as count
    FROM users WHERE role != 'admin'
    GROUP BY DATE_FORMAT(created_at, '%Y-%m')
    ORDER BY month DESC LIMIT 6
");

// Top users by lands
$top_users = mysqli_query($conn, "
    SELECT u.username, u.full_name, u.role, COUNT(l.id) as land_count, SUM(l.total_area) as total_area
    FROM users u
    LEFT JOIN lands l ON u.id = l.user_id
    WHERE u.role != 'admin'
    GROUP BY u.id
    ORDER BY land_count DESC
    LIMIT 10
");

// Alerts by status
$alert_stats = mysqli_query($conn, "
    SELECT status, COUNT(*) as count FROM overlap_alerts GROUP BY status
");
$alert_data = [];
while($row = mysqli_fetch_assoc($alert_stats)) {
    $alert_data[$row['status']] = $row['count'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reports - Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../assets/font-awesome/css/all.min.css">
    <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <!-- Page Header -->
        <div class="page-header">
            <div class="page-header-content">
                <div class="page-title">
                    <h1><i class="fas fa-chart-bar"></i> Reports & Statistics</h1>
                    <p>Comprehensive overview of system data and analytics</p>
                </div>
            </div>
        </div>

        <!-- Overview Stats -->
        <div class="stats-grid">
            <div class="stat-card gradient-purple">
                <div class="stat-icon">
                    <i class="fas fa-users"></i>
                </div>
                <div class="stat-content">
                    <h3><?php echo $total_users; ?></h3>
                    <p>Total Users</p>
                </div>
            </div>
            <div class="stat-card gradient-green">
                <div class="stat-icon">
                    <i class="fas fa-map-marked-alt"></i>
                </div>
                <div class="stat-content">
                    <h3><?php echo $total_lands; ?></h3>
                    <p>Total Lands</p>
                </div>
            </div>
            <div class="stat-card gradient-blue">
                <div class="stat-icon">
                    <i class="fas fa-ruler-combined"></i>
                </div>
                <div class="stat-content">
                    <h3><?php echo number_format($total_area / 10000, 1); ?></h3>
                    <p>Total Area (Ha)</p>
                </div>
            </div>
            <div class="stat-card gradient-orange">
                <div class="stat-icon">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <div class="stat-content">
                    <h3><?php echo $total_alerts; ?></h3>
                    <p>Total Alerts</p>
                </div>
            </div>
        </div>

        <div class="reports-grid">
            <!-- User Breakdown -->
            <div class="report-card">
                <div class="report-header">
                    <h3><i class="fas fa-users"></i> User Breakdown</h3>
                </div>
                <div class="report-body">
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Land Owners</span>
                            <span class="progress-value"><?php echo $total_owners; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar purple" style="width: <?php echo $total_users > 0 ? ($total_owners / $total_users * 100) : 0; ?>%">
                                <span><?php echo $total_users > 0 ? round($total_owners / $total_users * 100) : 0; ?>%</span>
                            </div>
                        </div>
                    </div>
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Brokers</span>
                            <span class="progress-value"><?php echo $total_brokers; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar blue" style="width: <?php echo $total_users > 0 ? ($total_brokers / $total_users * 100) : 0; ?>%">
                                <span><?php echo $total_users > 0 ? round($total_brokers / $total_users * 100) : 0; ?>%</span>
                            </div>
                        </div>
                    </div>
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Pending Approval</span>
                            <span class="progress-value"><?php echo $pending_users; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar orange" style="width: <?php echo $total_users > 0 ? ($pending_users / $total_users * 100) : 0; ?>%">
                                <span><?php echo $total_users > 0 ? round($pending_users / $total_users * 100) : 0; ?>%</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Alert Statistics -->
            <div class="report-card">
                <div class="report-header">
                    <h3><i class="fas fa-exclamation-triangle"></i> Alert Statistics</h3>
                </div>
                <div class="report-body">
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Pending</span>
                            <span class="progress-value"><?php echo $alert_data['pending'] ?? 0; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar orange" style="width: <?php echo $total_alerts > 0 ? (($alert_data['pending'] ?? 0) / $total_alerts * 100) : 0; ?>%"></div>
                        </div>
                    </div>
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Investigating</span>
                            <span class="progress-value"><?php echo $alert_data['investigating'] ?? 0; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar blue" style="width: <?php echo $total_alerts > 0 ? (($alert_data['investigating'] ?? 0) / $total_alerts * 100) : 0; ?>%"></div>
                        </div>
                    </div>
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Resolved</span>
                            <span class="progress-value"><?php echo $alert_data['resolved'] ?? 0; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar green" style="width: <?php echo $total_alerts > 0 ? (($alert_data['resolved'] ?? 0) / $total_alerts * 100) : 0; ?>%"></div>
                        </div>
                    </div>
                    <div class="progress-item">
                        <div class="progress-header">
                            <span class="progress-label">Dismissed</span>
                            <span class="progress-value"><?php echo $alert_data['dismissed'] ?? 0; ?></span>
                        </div>
                        <div class="progress-bar-container">
                            <div class="progress-bar gray" style="width: <?php echo $total_alerts > 0 ? (($alert_data['dismissed'] ?? 0) / $total_alerts * 100) : 0; ?>%"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Top Users -->
        <div class="content-card">
            <div class="card-header-custom">
                <h3><i class="fas fa-trophy"></i> Top Users by Land Count</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table modern-table">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>User</th>
                                <th>Role</th>
                                <th>Lands</th>
                                <th>Total Area</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $rank = 1; while($user = mysqli_fetch_assoc($top_users)): ?>
                            <tr>
                                <td>
                                    <?php if($rank <= 3): ?>
                                    <span class="rank-badge rank-<?php echo $rank; ?>">
                                        <i class="fas fa-trophy"></i> <?php echo $rank; ?>
                                    </span>
                                    <?php else: ?>
                                    <span class="rank-number"><?php echo $rank; ?></span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <strong><?php echo htmlspecialchars($user['full_name'] ?: $user['username']); ?></strong>
                                </td>
                                <td>
                                    <span class="role-badge role-<?php echo $user['role']; ?>">
                                        <?php echo ucfirst($user['role']); ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="land-count"><?php echo $user['land_count']; ?></span>
                                </td>
                                <td><?php echo number_format($user['total_area'], 2); ?> sqm</td>
                            </tr>
                            <?php $rank++; endwhile; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>

    <style>
    /* Page-specific styles */
    .page-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 20px;
        padding: 30px;
        margin-bottom: 30px;
        color: white;
    }

    .page-title h1 {
        font-size: 28px;
        font-weight: 700;
        margin: 0 0 8px 0;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .page-title p {
        margin: 0;
        opacity: 0.9;
        font-size: 15px;
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 20px;
        margin-bottom: 30px;
    }

    .stat-card {
        border-radius: 20px;
        padding: 28px;
        display: flex;
        align-items: center;
        gap: 20px;
        color: white;
        position: relative;
        overflow: hidden;
    }

    .stat-card::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -50%;
        width: 100%;
        height: 100%;
        background: rgba(255,255,255,0.1);
        border-radius: 50%;
    }

    .gradient-purple {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    }

    .gradient-green {
        background: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
    }

    .gradient-blue {
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
    }

    .gradient-orange {
        background: linear-gradient(135deg, #f5af19 0%, #f12711 100%);
    }

    .stat-card .stat-icon {
        width: 60px;
        height: 60px;
        border-radius: 16px;
        background: rgba(255,255,255,0.2);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 26px;
    }

    .stat-card .stat-content h3 {
        font-size: 32px;
        font-weight: 700;
        margin: 0;
    }

    .stat-card .stat-content p {
        margin: 4px 0 0 0;
        opacity: 0.9;
        font-size: 14px;
    }

    .reports-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
        gap: 24px;
        margin-bottom: 30px;
    }

    .report-card {
        background: white;
        border-radius: 20px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        overflow: hidden;
    }

    .report-header {
        padding: 24px;
        border-bottom: 1px solid #f1f5f9;
    }

    .report-header h3 {
        font-size: 18px;
        font-weight: 600;
        margin: 0;
        color: #1e293b;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .report-body {
        padding: 24px;
    }

    .progress-item {
        margin-bottom: 20px;
    }

    .progress-item:last-child {
        margin-bottom: 0;
    }

    .progress-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 8px;
    }

    .progress-label {
        font-size: 14px;
        color: #475569;
        font-weight: 500;
    }

    .progress-value {
        font-size: 14px;
        font-weight: 600;
        color: #1e293b;
    }

    .progress-bar-container {
        height: 10px;
        background: #f1f5f9;
        border-radius: 10px;
        overflow: hidden;
    }

    .progress-bar {
        height: 100%;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        padding-right: 8px;
        min-width: 30px;
        transition: width 0.5s ease;
    }

    .progress-bar span {
        font-size: 9px;
        color: white;
        font-weight: 600;
    }

    .progress-bar.purple {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    }

    .progress-bar.blue {
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
    }

    .progress-bar.green {
        background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
    }

    .progress-bar.orange {
        background: linear-gradient(135deg, #f5af19 0%, #f59e0b 100%);
    }

    .progress-bar.gray {
        background: #94a3b8;
    }

    .content-card {
        background: white;
        border-radius: 20px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        overflow: hidden;
    }

    .card-header-custom {
        padding: 24px;
        border-bottom: 1px solid #f1f5f9;
    }

    .card-header-custom h3 {
        font-size: 18px;
        font-weight: 600;
        margin: 0;
        color: #1e293b;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .card-body {
        padding: 0;
    }

    .modern-table {
        margin: 0;
    }

    .modern-table thead th {
        background: #f8fafc;
        border: none;
        padding: 16px 24px;
        font-size: 12px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        color: #64748b;
    }

    .modern-table tbody td {
        padding: 16px 24px;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }

    .modern-table tbody tr:hover {
        background: #f8fafc;
    }

    .rank-badge {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 6px 12px;
        border-radius: 10px;
        font-size: 13px;
        font-weight: 600;
    }

    .rank-1 {
        background: linear-gradient(135deg, #f5af19 0%, #f59e0b 100%);
        color: white;
    }

    .rank-2 {
        background: linear-gradient(135deg, #94a3b8 0%, #64748b 100%);
        color: white;
    }

    .rank-3 {
        background: linear-gradient(135deg, #cd7f32 0%, #a0522d 100%);
        color: white;
    }

    .rank-number {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 32px;
        height: 32px;
        background: #f1f5f9;
        border-radius: 8px;
        font-weight: 600;
        color: #64748b;
    }

    .role-badge {
        display: inline-block;
        padding: 6px 14px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 600;
    }

    .role-broker {
        background: linear-gradient(135deg, #06b6d4 0%, #0891b2 100%);
        color: white;
    }

    .role-user {
        background: #e2e8f0;
        color: #475569;
    }

    .land-count {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 10px;
        font-weight: 700;
        color: white;
    }

    @media (max-width: 768px) {
        .page-header {
            padding: 20px;
        }

        .page-title h1 {
            font-size: 22px;
        }

        .stats-grid {
            grid-template-columns: 1fr 1fr;
        }

        .stat-card {
            padding: 20px;
        }

        .stat-card .stat-icon {
            width: 48px;
            height: 48px;
            font-size: 20px;
        }

        .stat-card .stat-content h3 {
            font-size: 24px;
        }

        .reports-grid {
            grid-template-columns: 1fr;
        }

        .modern-table thead th,
        .modern-table tbody td {
            padding: 12px 16px;
        }
    }
    </style>

    <script src="../assets/vendor/jquery/jquery.min.js"></script>
    <script src="../assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>
