<?php
// Start session and include necessary files
session_start();
require_once '../config.php';
require_once '../includes/auth.php';
require_once '../classes/User.php';
require_once '../classes/Announcement.php';
require_once '../classes/DataSource.php'; // Still needed for overall data source counts
require_once '../classes/Classifications.php'; // For classification counts
require_once '../classes/Aboutus.php';
require_once '../classes/Contactus.php'; // For feedback/contact messages
require_once '../classes/Faq.php';
require_once '../classes/Slide.php'; // Include Slide class

// Redirect if not logged in or not a DAC Staff
redirect_if_not_logged_in('../index.php');
redirect_if_not_role('DAC Staff', '../index.php');

// Initialize classes
$user = new User($pdo);
$announcement = new Announcement($pdo);
$dataSource = new DataSource($pdo);
$classification = new Classifications($pdo);
$aboutUs = new Aboutus($pdo);
$contactUs = new Contactus($pdo);
$faq = new Faq($pdo);
$slideManager = new Slide($pdo);

// Fetch dashboard data
$totalUsers = $user->getTotalUsers();
$totalAnnouncements = $announcement->getTotalAnnouncements();
$totalDataSources = $dataSource->getTotalDataSources();
$totalCategories = $classification->getTotalCategories();
$totalDataTypes = $classification->getTotalDataTypes();
$totalFeedback = $contactUs->getTotalFeedback();
$totalFaqs = $faq->getTotalFaqs();
$totalSlides = $slideManager->getTotalSlides();
$pendingPermissions = $dataSource->getPendingPermissionRequestsCount(); // This correctly gets ALL pending for admin dashboard
?>

<!DOCTYPE html>
<html lang="en">
<!-- Header -->
<?php
// Include header file for admin pages
include_once("../includes/header_admin.php");
?>

<body>
    <div class="wrapper">
        <!-- Sidebar -->
        <?php
        // Include header file for admin pages
        include_once("../includes/nav_admin.php");
        ?>
        <!-- Main Content -->
        <div class="main-content">
            <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4 rounded-3">
                <div class="container-fluid">
                    <a class="navbar-brand" href="#">Admin Dashboard</a>
                    <div class="d-flex">
                        <span class="navbar-text me-3">
                            Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!
                        </span>
                    </div>
                </div>
            </nav>

            <?php
            // Display session messages
            if (isset($_SESSION['message'])) {
                echo '<div class="alert alert-' . $_SESSION['message_type'] . ' alert-dismissible fade show rounded-pill" role="alert">' . htmlspecialchars($_SESSION['message']) . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
                unset($_SESSION['message']);
                unset($_SESSION['message_type']);
            }
            ?>

            <div class="row g-4">
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-users card-icon mb-3"></i>
                            <h5 class="card-title">Total Users</h5>
                            <p class="card-text fs-2 fw-bold text-primary"><?php echo $totalUsers; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-bullhorn card-icon mb-3"></i>
                            <h5 class="card-title">Total Announcements</h5>
                            <p class="card-text fs-2 fw-bold text-success"><?php echo $totalAnnouncements; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-database card-icon mb-3"></i>
                            <h5 class="card-title">Total Data Sources</h5>
                            <p class="card-text fs-2 fw-bold text-info"><?php echo $totalDataSources; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-folder-open card-icon mb-3"></i>
                            <h5 class="card-title">Data Categories</h5>
                            <p class="card-text fs-2 fw-bold text-warning"><?php echo $totalCategories; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-file-alt card-icon mb-3"></i>
                            <h5 class="card-title">Data Types</h5>
                            <p class="card-text fs-2 fw-bold text-danger"><?php echo $totalDataTypes; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-handshake card-icon mb-3"></i>
                            <h5 class="card-title">Pending Permissions</h5>
                            <p class="card-text fs-2 fw-bold text-secondary"><?php echo $pendingPermissions; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-comments card-icon mb-3"></i>
                            <h5 class="card-title">New Feedback</h5>
                            <p class="card-text fs-2 fw-bold text-primary"><?php echo $totalFeedback; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-question card-icon mb-3"></i>
                            <h5 class="card-title">Total FAQs</h5>
                            <p class="card-text fs-2 fw-bold text-success"><?php echo $totalFaqs; ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <div class="card text-center p-4">
                        <div class="card-body">
                            <i class="fas fa-images card-icon mb-3"></i>
                            <h5 class="card-title">Total Slides</h5>
                            <p class="card-text fs-2 fw-bold text-warning"><?php echo $totalSlides; ?></p>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <!-- Footer -->
    <?php
    // Include Footer file for owner pages
    include_once("../includes/footer_admin.php");
    ?>
</body>
</html>
