<?php

declare(strict_types=1);

require_once __DIR__ . '/../library/auth.php';

allow_roles(['admin', 'accountant', 'teacher']);

$page_title = "Religion Report";

$school_id = (int) $_SESSION['school_id'];

$current_session_id = get_current_session_id($pdo);

$class_id = (int) ($_GET['class_id'] ?? 0);

$religion_report = [];

$minority_report = [];

$total_students = 0;

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/

$classStmt = $pdo->prepare("

    SELECT

        id,
        class_name,
        section

    FROM classes

    WHERE school_id = ?

    ORDER BY sort_order ASC,
    class_name ASC

");

$classStmt->execute([

    $school_id

]);

$classes = $classStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH REPORT DATA
|--------------------------------------------------------------------------
*/

if ($class_id > 0) {

    $stmt = $pdo->prepare("

        SELECT

            sci.religion,
            sci.minority_group

        FROM student_sessions ss

        INNER JOIN students s
            ON s.id = ss.student_id

        LEFT JOIN student_category_info sci
            ON sci.student_id = s.id

        WHERE ss.school_id = ?
        AND ss.session_id = ?
        AND ss.class_id = ?
        AND ss.status = 'Active'

    ");

    $stmt->execute([

        $school_id,
        $current_session_id,
        $class_id

    ]);

    $students =
        $stmt->fetchAll(PDO::FETCH_ASSOC);

    /*
    |--------------------------------------------------------------------------
    | CALCULATIONS
    |--------------------------------------------------------------------------
    */

    foreach ($students as $student) {

        $total_students++;

        /*
        |--------------------------------------------------------------------------
        | RELIGION
        |--------------------------------------------------------------------------
        */

        $religion =
            trim($student['religion'] ?? '');

        if ($religion === '') {

            $religion = 'Unknown';
        }

        if (!isset($religion_report[$religion])) {

            $religion_report[$religion] = 0;
        }

        $religion_report[$religion]++;

        /*
        |--------------------------------------------------------------------------
        | MINORITY GROUP
        |--------------------------------------------------------------------------
        */

        $minority =
            trim($student['minority_group'] ?? '');

        if ($minority === '') {

            $minority = 'Unknown';
        }

        if (!isset($minority_report[$minority])) {

            $minority_report[$minority] = 0;
        }

        $minority_report[$minority]++;
    }
}

require_once __DIR__ . '/../include/header.php';

require_once __DIR__ . '/../include/sidebar.php';

?>

<style>

.modern-card {

    border: none;

    border-radius: 18px;

    overflow: hidden;

    box-shadow: 0 4px 18px rgba(0,0,0,0.06);
}

.modern-card .card-header {

    background: #fff;

    border-bottom: 1px solid #eef2f7;

    padding: 18px 22px;
}

.filter-box {

    background: linear-gradient(135deg,#1e293b,#0f172a);

    border-radius: 18px;

    padding: 30px;

    color: #fff;

    margin-bottom: 25px;
}

.table-modern thead {

    background: #111827;

    color: #fff;
}

.table-modern th {

    border: none !important;

    font-size: 14px;

    font-weight: 600;
}

.stat-box {

    border-radius: 18px;

    padding: 25px;

    color: #fff;

    background: linear-gradient(135deg,#7c3aed,#6d28d9);

    margin-bottom: 25px;
}

.empty-state {

    text-align: center;

    padding: 70px 20px;
}

.empty-state i {

    font-size: 70px;

    color: #cbd5e1;

    margin-bottom: 20px;
}

</style>

<div class="content-wrapper">

    <!-- HEADER -->

    <section class="content-header">

        <div class="container-fluid">

            <div class="filter-box">

                <div class="row align-items-center">

                    <div class="col-lg-8">

                        <h1 class="font-weight-bold mb-2">

                            Religion Report 🕌

                        </h1>

                        <p
                            class="mb-0"
                            style="
                                color: rgba(255,255,255,0.75);
                            "
                        >

                            Analyze religion and minority
                            group distribution of students.

                        </p>

                    </div>

                    <div
                        class="col-lg-4 text-lg-right mt-4 mt-lg-0"
                    >

                        <span
                            class="badge badge-light p-3"
                            style="
                                font-size:14px;
                                border-radius:12px;
                            "
                        >

                            School Analytics

                        </span>

                    </div>

                </div>

            </div>

        </div>

    </section>

    <!-- CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- FILTER -->

            <div class="card modern-card mb-4">

                <div class="card-header">

                    <h3 class="card-title">

                        Filter Religion Report

                    </h3>

                </div>

                <div class="card-body">

                    <form method="GET">

                        <div class="row align-items-end">

                            <div class="col-md-4">

                                <label>

                                    Select Class

                                </label>

                                <select
                                    name="class_id"
                                    class="form-control"
                                    required
                                >

                                    <option value="">

                                        Choose Class

                                    </option>

                                    <?php foreach ($classes as $class): ?>

                                        <option
                                            value="<?= (int) $class['id']; ?>"

                                            <?php
                                            if (
                                                $class_id ===
                                                (int) $class['id']
                                            ) {
                                                echo 'selected';
                                            }
                                            ?>
                                        >

                                            <?= htmlspecialchars(
                                                $class['class_name']
                                            ); ?>

                                            <?php if (!empty($class['section'])): ?>

                                                -
                                                <?= htmlspecialchars(
                                                    $class['section']
                                                ); ?>

                                            <?php endif; ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                            <div class="col-md-3">

                                <button
                                    type="submit"
                                    class="btn btn-primary btn-block"
                                >

                                    <i class="fas fa-search"></i>

                                    Generate Report

                                </button>

                            </div>

                            <?php if ($class_id > 0): ?>

                                <div class="col-md-2">

                                    <a
                                        href="download_report.php?type=religion&class_id=<?= $class_id; ?>"
                                        class="btn btn-success btn-block"
                                    >

                                        <i class="fas fa-file-excel"></i>

                                        Excel

                                    </a>

                                </div>

                                <div class="col-md-2">

                                    <button
                                        type="button"
                                        onclick="window.print()"
                                        class="btn btn-dark btn-block"
                                    >

                                        <i class="fas fa-print"></i>

                                        Print

                                    </button>

                                </div>

                            <?php endif; ?>

                        </div>

                    </form>

                </div>

            </div>

            <!-- STATS -->

            <?php if ($class_id > 0): ?>

                <div class="stat-box">

                    <h2 class="font-weight-bold">

                        <?= number_format($total_students); ?>

                    </h2>

                    <p class="mb-0">

                        Total Active Students

                    </p>

                </div>

            <?php endif; ?>

            <!-- REPORT TABLES -->

            <?php if ($class_id > 0): ?>

                <div class="row">

                    <!-- RELIGION -->

                    <div class="col-lg-6 mb-4">

                        <div class="card modern-card h-100">

                            <div class="card-header">

                                <h3 class="card-title">

                                    Religion Distribution

                                </h3>

                            </div>

                            <div class="card-body p-0">

                                <div class="table-responsive">

                                    <table
                                        class="table table-bordered table-hover table-modern mb-0"
                                    >

                                        <thead>

                                            <tr>

                                                <th>#</th>

                                                <th>Religion</th>

                                                <th>Total Students</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <?php
                                            $i = 1;

                                            foreach (
                                                $religion_report
                                                as $religion => $count
                                            ):
                                            ?>

                                                <tr>

                                                    <td>

                                                        <?= $i++; ?>

                                                    </td>

                                                    <td>

                                                        <?= htmlspecialchars(
                                                            $religion
                                                        ); ?>

                                                    </td>

                                                    <td>

                                                        <span
                                                            class="badge badge-primary p-2"
                                                        >

                                                            <?= $count; ?>

                                                        </span>

                                                    </td>

                                                </tr>

                                            <?php endforeach; ?>

                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        </div>

                    </div>

                    <!-- MINORITY -->

                    <div class="col-lg-6 mb-4">

                        <div class="card modern-card h-100">

                            <div class="card-header">

                                <h3 class="card-title">

                                    Minority Group Distribution

                                </h3>

                            </div>

                            <div class="card-body p-0">

                                <div class="table-responsive">

                                    <table
                                        class="table table-bordered table-hover table-modern mb-0"
                                    >

                                        <thead>

                                            <tr>

                                                <th>#</th>

                                                <th>Minority Group</th>

                                                <th>Total Students</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <?php
                                            $i = 1;

                                            foreach (
                                                $minority_report
                                                as $minority => $count
                                            ):
                                            ?>

                                                <tr>

                                                    <td>

                                                        <?= $i++; ?>

                                                    </td>

                                                    <td>

                                                        <?= htmlspecialchars(
                                                            $minority
                                                        ); ?>

                                                    </td>

                                                    <td>

                                                        <span
                                                            class="badge badge-success p-2"
                                                        >

                                                            <?= $count; ?>

                                                        </span>

                                                    </td>

                                                </tr>

                                            <?php endforeach; ?>

                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            <?php else: ?>

                <div class="card modern-card">

                    <div class="card-body">

                        <div class="empty-state">

                            <i class="fas fa-chart-bar"></i>

                            <h4>

                                No Report Generated

                            </h4>

                            <p class="text-muted">

                                Please select a class to
                                generate religion report.

                            </p>

                        </div>

                    </div>

                </div>

            <?php endif; ?>

        </div>

    </section>

</div>

<?php require_once __DIR__ . '/../include/footer.php'; ?>