<?php
// File: /admin/index.php
include 'header.php'; // Includes security check, db connection, and starts session

// Check for a valid database connection
if (!isset($conn) || $conn->connect_error) {
    die("<div class='p-8 text-red-600 bg-red-100'>Database connection failed. Please check your db.php file and admin/header.php include path.</div>");
}

// --- DYNAMIC DATA FETCHING FOR ADMIN ---

// 1. Fetch Total Users
$totalUsers = 0;
$result_users = $conn->query("SELECT COUNT(id) as user_count FROM users");
if ($result_users) {
    $totalUsers = $result_users->fetch_assoc()['user_count'];
}


// 2. Fetch Total Artists
$totalArtists = 0;
$result_artists = $conn->query("SELECT COUNT(id) as artist_count FROM users WHERE role = 'artist'");
if ($result_artists) {
    $totalArtists = $result_artists->fetch_assoc()['artist_count'];
}


// 3. Fetch Total Paintings
$totalPaintings = 0;
$result_paintings = $conn->query("SELECT COUNT(id) as painting_count FROM paintings");
if ($result_paintings) {
    $totalPaintings = $result_paintings->fetch_assoc()['painting_count'];
}


// 4. Fetch Total Sales Revenue
$totalSales = 0;
$result_sales = $conn->query("SELECT SUM(total_amount) as total_revenue FROM orders WHERE payment_status = 'completed'");
if ($result_sales) {
    $totalSalesData = $result_sales->fetch_assoc();
    $totalSales = $totalSalesData['total_revenue'] ?? 0;
}

?>

<!-- Main Content -->
<div class="p-6 md:p-8 bg-gray-50 min-h-screen">
    <!-- 1. Welcome Header -->
    <div class="mb-8">
        <h1 class="text-3xl font-bold text-ucf-charcoal">Admin Dashboard</h1>
        <p class="text-lg text-gray-600">Overview of the United Cultural Forum platform.</p>
    </div>

    <!-- 2. Colorful Stats Cards Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">

        <!-- Total Users Card -->
        <div class="relative p-6 rounded-lg shadow-lg text-white bg-gradient-to-br from-ucf-green to-teal-600 overflow-hidden">
            <i class="fas fa-users absolute -right-4 -bottom-4 text-white opacity-20 text-8xl"></i>
            <p class="text-sm font-medium uppercase tracking-wider">Total Users</p>
            <p class="text-4xl font-bold mt-2"><?php echo $totalUsers; ?></p>
        </div>

        <!-- Total Artists Card -->
        <div class="relative p-6 rounded-lg shadow-lg text-white bg-gradient-to-br from-blue-500 to-indigo-600 overflow-hidden">
             <i class="fas fa-paint-brush absolute -right-4 -bottom-4 text-white opacity-20 text-8xl"></i>
            <p class="text-sm font-medium uppercase tracking-wider">Registered Artists</p>
            <p class="text-4xl font-bold mt-2"><?php echo $totalArtists; ?></p>
        </div>

        <!-- Total Paintings Card -->
        <div class="relative p-6 rounded-lg shadow-lg text-white bg-gradient-to-br from-yellow-500 to-amber-600 overflow-hidden">
             <i class="fas fa-palette absolute -right-4 -bottom-4 text-white opacity-20 text-8xl"></i>
            <p class="text-sm font-medium uppercase tracking-wider">Total Paintings</p>
            <p class="text-4xl font-bold mt-2"><?php echo $totalPaintings; ?></p>
        </div>
        
        <!-- Total Revenue Card -->
        <div class="relative p-6 rounded-lg shadow-lg text-white bg-gradient-to-br from-ucf-charcoal to-gray-800 overflow-hidden">
             <i class="fas fa-dollar-sign absolute -right-4 -bottom-4 text-white opacity-20 text-8xl"></i>
            <p class="text-sm font-medium uppercase tracking-wider">Total Revenue</p>
            <p class="text-4xl font-bold mt-2">₹<?php echo number_format($totalSales, 2); ?></p>
        </div>

    </div> <!-- / End Stats Grid -->

     <!-- 3. Quick Actions & Recent Activity -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- Quick Actions -->
        <div class="lg:col-span-1 bg-white p-6 rounded-lg shadow-md">
            <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Quick Actions</h2>
            <div class="flex flex-col space-y-3">
                <a href="manage_users.php" class="bg-ucf-green text-white font-bold py-3 px-4 rounded-lg hover:bg-ucf-green-dark transition-all duration-300 transform hover:scale-105 flex items-center justify-center">
                    <i class="fas fa-user-cog mr-2"></i> Manage Users
                </a>
                <a href="competitions.php" class="bg-ucf-charcoal text-white font-bold py-3 px-4 rounded-lg hover:bg-gray-700 transition-all duration-300 transform hover:scale-105 flex items-center justify-center">
                    <i class="fas fa-trophy mr-2"></i> Manage Competitions
                </a>
                 <a href="articles.php" class="bg-gray-200 text-ucf-charcoal font-bold py-3 px-4 rounded-lg hover:bg-gray-300 transition-all duration-300 transform hover:scale-105 flex items-center justify-center">
                    <i class="fas fa-newspaper mr-2"></i> Manage Articles
                </a>
            </div>
        </div>

        <!-- Placeholder for recent activity -->
        <div class="lg:col-span-2 bg-white p-6 rounded-lg shadow-md">
            <h2 class="text-2xl font-bold text-ucf-charcoal mb-4">Platform Analytics</h2>
            <p class="text-gray-600">Graphs and recent activity logs will be displayed here in a future update.</p>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>

