<?php
session_start();

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

date_default_timezone_set('Asia/Kolkata');

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../app/helpers.php';

// Load configuration
$appConfig = require_once __DIR__ . '/../config/app.php';
define('APP_URL', $appConfig['url']);

spl_autoload_register(function ($class) {
    $prefix = 'App\\';
    $base_dir = __DIR__ . '/../app/';
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) return;
    $relative_class = substr($class, $len);
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    if (file_exists($file)) require $file;
});

use App\Core\Router;

$router = new Router();

// Routes
$router->add('/', ['HomeController', 'index']);
$router->add('/login', ['AuthController', 'login']);
$router->add('/admin/login', ['AuthController', 'adminLogin']);
$router->add('/staff/login', ['AuthController', 'staffLogin']);
$router->add('/logout', ['AuthController', 'logout']);
$router->add('/dashboard', ['DashboardController', 'index']);
$router->add('/staff/dashboard', ['DashboardController', 'index']);

// Staff Workspaces (Dedicated Prefixes)
$router->add('/staff/leads', ['LeadController', 'index']);
$router->add('/staff/leads/view', ['LeadController', 'view']);
$router->add('/staff/leads/save', ['LeadController', 'save']);
$router->add('/staff/leads/delete', ['LeadController', 'delete']);
$router->add('/staff/leads/followup', ['LeadController', 'addFollowup']);

$router->add('/staff/students', ['StudentController', 'index']);
$router->add('/staff/students/create', ['StudentController', 'create']);
$router->add('/staff/students/edit', ['StudentController', 'edit']);
$router->add('/staff/students/view', ['StudentController', 'view']);
$router->add('/staff/students/save', ['StudentController', 'save']);
$router->add('/staff/students/delete', ['StudentController', 'delete']);
$router->add('/staff/students/toggle-freeze', ['StudentController', 'toggleFreeze']);
$router->add('/staff/students/reset-password', ['StudentController', 'resetPassword']);

$router->add('/staff/admissions', ['AdmissionController', 'index']);
$router->add('/staff/admissions/save', ['AdmissionController', 'save']);
$router->add('/staff/admissions/delete', ['AdmissionController', 'delete']);
$router->add('/staff/admissions/download', ['AdmissionController', 'downloadPdf']);

$router->add('/staff/attendance', ['AttendanceController', 'index']);
$router->add('/staff/attendance/save', ['AttendanceController', 'save']);
$router->add('/staff/attendance/report', ['AttendanceController', 'report']);
$router->add('/staff/attendance/export', ['AttendanceController', 'exportPdf']);

$router->add('/staff/teachers', ['TeacherController', 'index']);
$router->add('/staff/teachers/save', ['TeacherController', 'save']);
$router->add('/staff/teachers/delete', ['TeacherController', 'delete']);
$router->add('/staff/courses', ['CourseController', 'index']);
$router->add('/staff/courses/save', ['CourseController', 'save']);
$router->add('/staff/courses/delete', ['CourseController', 'delete']);
$router->add('/staff/batches', ['BatchController', 'index']);
$router->add('/staff/batches/save', ['BatchController', 'save']);
$router->add('/staff/batches/delete', ['BatchController', 'delete']);
$router->add('/staff/study-materials', ['StudyMaterialController', 'index']);
$router->add('/staff/study-materials/save', ['StudyMaterialController', 'save']);
$router->add('/staff/study-materials/delete', ['StudyMaterialController', 'delete']);
$router->add('/staff/study-materials/toggle-status', ['StudyMaterialController', 'toggleStatus']);
$router->add('/staff/categories', ['CategoryController', 'index']);
$router->add('/staff/categories/save', ['CategoryController', 'save']);
$router->add('/staff/categories/toggle-status', ['CategoryController', 'toggleStatus']);
$router->add('/staff/categories/delete', ['CategoryController', 'delete']);
$router->add('/staff/fee-heads', ['FeeHeadController', 'index']);
$router->add('/staff/fee-heads/save', ['FeeHeadController', 'save']);
$router->add('/staff/fee-heads/delete', ['FeeHeadController', 'delete']);
$router->add('/staff/fee-heads/toggle-status', ['FeeHeadController', 'toggleStatus']);
$router->add('/staff/fees', ['FeeController', 'index']);
$router->add('/staff/fees/view', ['FeeController', 'view']);
$router->add('/staff/fees/collect', ['FeeController', 'collect']);
$router->add('/staff/fees/generate', ['FeeController', 'generateInvoices']);
$router->add('/staff/expenses', ['ExpenseController', 'index']);
$router->add('/staff/expenses/save', ['ExpenseController', 'save']);
$router->add('/staff/expenses/delete', ['ExpenseController', 'delete']);
$router->add('/staff/expense-categories', ['ExpenseCategoryController', 'index']);
$router->add('/staff/expense-categories/save', ['ExpenseCategoryController', 'save']);
$router->add('/staff/expense-categories/delete', ['ExpenseCategoryController', 'delete']);
$router->add('/staff/users', ['UserController', 'index']);
$router->add('/staff/users/save', ['UserController', 'save']);
$router->add('/staff/users/delete', ['UserController', 'delete']);

// Study Materials Master
$router->add('/admin/study-materials', ['StudyMaterialController', 'index']);
$router->add('/admin/study-materials/save', ['StudyMaterialController', 'save']);
$router->add('/admin/study-materials/delete', ['StudyMaterialController', 'delete']);
$router->add('/admin/study-materials/toggle-status', ['StudyMaterialController', 'toggleStatus']);

// Student Attendance
$router->add('/admin/attendance', ['AttendanceController', 'index']);
$router->add('/admin/attendance/save', ['AttendanceController', 'save']);
$router->add('/admin/attendance/report', ['AttendanceController', 'report']);
$router->add('/admin/attendance/export', ['AttendanceController', 'exportPdf']);

// Admissions
$router->add('/admin/admissions', ['AdmissionController', 'index']);
$router->add('/admin/admissions/save', ['AdmissionController', 'save']);
$router->add('/admin/admissions/delete', ['AdmissionController', 'delete']);
$router->add('/admin/admissions/download', ['AdmissionController', 'downloadPdf']);


// Lead Management CRM
$router->add('/admin/leads', ['LeadController', 'index']);
$router->add('/admin/leads/view', ['LeadController', 'view']);
$router->add('/admin/leads/save', ['LeadController', 'save']);
$router->add('/admin/leads/delete', ['LeadController', 'delete']);
$router->add('/admin/leads/followup', ['LeadController', 'addFollowup']);

// Students CRM
$router->add('/admin/students', ['StudentController', 'index']);
$router->add('/admin/students/create', ['StudentController', 'create']);
$router->add('/admin/students/edit', ['StudentController', 'edit']);
$router->add('/admin/students/view', ['StudentController', 'view']);
$router->add('/admin/students/save', ['StudentController', 'save']);
$router->add('/admin/students/delete', ['StudentController', 'delete']);
$router->add('/admin/students/reset-password', ['StudentController', 'resetPassword']);
$router->add('/admin/students/toggle-freeze', ['StudentController', 'toggleFreeze']);

// Batches Master
$router->add('/admin/batches', ['BatchController', 'index']);
$router->add('/admin/batches/save', ['BatchController', 'save']);
$router->add('/admin/batches/delete', ['BatchController', 'delete']);

// Courses Master
$router->add('/admin/courses', ['CourseController', 'index']);
$router->add('/admin/courses/save', ['CourseController', 'save']);
$router->add('/admin/courses/delete', ['CourseController', 'delete']);

// Teachers Master
$router->add('/admin/teachers', ['TeacherController', 'index']);
$router->add('/admin/teachers/save', ['TeacherController', 'save']);
$router->add('/admin/teachers/delete', ['TeacherController', 'delete']);

// Admin Categories
$router->add('/admin/categories', ['CategoryController', 'index']);
$router->add('/admin/categories/save', ['CategoryController', 'save']);
$router->add('/admin/categories/toggle-status', ['CategoryController', 'toggleStatus']);
$router->add('/admin/categories/delete', ['CategoryController', 'delete']);

// Fees Head
$router->add('/admin/fee-heads', ['FeeHeadController', 'index']);
$router->add('/admin/fee-heads/save', ['FeeHeadController', 'save']);
$router->add('/admin/fee-heads/delete', ['FeeHeadController', 'delete']);
$router->add('/admin/fee-heads/toggle-status', ['FeeHeadController', 'toggleStatus']);

// Admin Settings & Profile
$router->add('/admin/settings', ['SettingsController', 'index']);
$router->add('/admin/settings/save', ['SettingsController', 'save']);
$router->add('/admin/profile', ['SettingsController', 'profile']);
$router->add('/admin/profile/update', ['SettingsController', 'updateProfile']);

// Staff Management
$router->add('/admin/users', ['UserController', 'index']);
$router->add('/admin/users/save', ['UserController', 'save']);
$router->add('/admin/users/delete', ['UserController', 'delete']);

// Financial Module - Invoices
$router->add('/admin/invoices/download', ['InvoiceController', 'downloadPdf']);

// Unified Fees Module
$router->add('/admin/fees', ['FeeController', 'index']);
$router->add('/admin/fees/view', ['FeeController', 'view']);
$router->add('/admin/fees/collect', ['FeeController', 'collect']);
$router->add('/admin/fees/generate', ['FeeController', 'generateInvoices']);

// Expenses Module
$router->add('/admin/expenses', ['ExpenseController', 'index']);
$router->add('/admin/expenses/save', ['ExpenseController', 'save']);
$router->add('/admin/expenses/delete', ['ExpenseController', 'delete']);

// Certificate Generator
$router->add('/admin/certificates', ['CertificateController', 'index']);
$router->add('/admin/certificates/create', ['CertificateController', 'create']);
$router->add('/admin/certificates/generate', ['CertificateController', 'generate']);
$router->add('/admin/certificates/download', ['CertificateController', 'download']);

// Expense Categories
$router->add('/admin/expense-categories', ['ExpenseCategoryController', 'index']);
$router->add('/admin/expense-categories/save', ['ExpenseCategoryController', 'save']);
$router->add('/admin/expense-categories/delete', ['ExpenseCategoryController', 'delete']);


// Student Portal
$router->add('/student/courses', ['StudentPortalController', 'courses']);
$router->add('/student/fees', ['StudentPortalController', 'fees']);
$router->add('/student/materials', ['StudentPortalController', 'materials']);
$router->add('/student/materials/download', ['StudentPortalController', 'download']);
$router->add('/student/profile', ['StudentPortalController', 'profile']);

$router->run();
