<?php
// index.php
session_start();

// Temporary debug mode for cPanel deployment issues
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!file_exists('config/database.php') || filesize('config/database.php') < 100) {
    header("Location: setup.php");
    exit;
}

require_once 'config/database.php';
require_once 'includes/functions.php';
require_once 'includes/version.php';

// Simple Router
$page = isset($_GET['page']) ? clean_input($_GET['page']) : 'login';

// Whitelist allowed pages for security
$allowed_pages = [
    'login', 
    'dashboard', 
    'members', 
    'member_form', 
    'services', 
    'roster',
    'roster_view',
    'roster_export_view',
    'attendance_kiosk',
    'attendance_kiosk_manual',
    'attendance_manual',
    'issues', 
    'issue_form',
    'reports', 
    'attendance_monthly',
    'settings',
    'service_attendance_detail',
    'attendance_monthly',
    'settings',
    'service_attendance_detail',
    'users',
    'worship_reports',
    'worship_report_form',
    'worship_report_view',
    'worship_review',
    'streaming_dashboard',
    'streaming_input',
    'streaming_reports',
    'streaming_config',
    'public_calendar',
    'calendar_manager',
    'app_functions_permissions',
    'app_functions_permissions',
    'user_guide',
    'activity_logs',
    'print_form',
    'birthdays'
];

// If page is not in whitelist, default to login
if (!in_array($page, $allowed_pages)) {
    $page = 'login';
}

// Check for login on protected pages
if ($page !== 'login' && $page !== 'public_calendar') {
    requireLogin();
}

$page_file = "pages/{$page}.php";

// Standalone Pages (No Header/Footer/CSS)
if ($page === 'print_form') {
    if (file_exists($page_file)) {
        include $page_file;
    } else {
        echo "404 Not Found";
    }
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WCS Admin - <?php echo ucfirst($page); ?></title>
    <!-- Simple CSS Reset & Variables -->
    <link rel="stylesheet" href="assets/css/style.css">
    <!-- Font Awesome (Optional, using CDN for now) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- html2canvas for JPG export -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body class="<?php echo $page; ?>-page">

    <?php 
    $is_print_view = ($page === 'roster' && isset($_GET['view']) && $_GET['view'] === 'print');
    $is_public_view = ($page === 'public_calendar');

    if ($page !== 'login' && $page !== 'attendance_kiosk' && $page !== 'attendance_kiosk_manual' && !$is_print_view && !$is_public_view): ?>
        <?php include 'includes/header.php'; ?>
    <?php endif; ?>

    <main class="container">
        <?php
        if (file_exists($page_file)) {
            include $page_file;
        } else {
            echo "<h1>404 - Page Not Found</h1><p>The page <strong>{$page}</strong> does not exist.</p>";
        }
        ?>
    </main>

    <?php if ($page !== 'login' && $page !== 'attendance_kiosk' && $page !== 'attendance_kiosk_manual' && !$is_print_view && !$is_public_view): ?>
        <?php include 'includes/footer.php'; ?>
    <?php endif; ?>

    <script src="assets/js/script.js"></script>
</body>
</html>
