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

require_once 'config/database.php';

// Simple Router
$request = $_SERVER['REQUEST_URI'];
$basePath = ''; // Adjust if running in a subdirectory

// Remove base path from request if necessary and parse URL
$path = parse_url($request, PHP_URL_PATH);
$path = str_replace($basePath, '', $path);

// Check if user is logged in
$isLoggedIn = isset($_SESSION['user_id']);

if (!$isLoggedIn && $path !== '/login') {
    header('Location: /login');
    exit;
}

switch ($path) {
    case '/':
    case '/dashboard':
        require 'views/dashboard/index.php';
        break;
    case '/login':
        require 'controllers/AuthController.php';
        $auth = new AuthController();
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $auth->login();
        } else {
            require 'views/auth/login.php';
        }
        break;
    case '/logout':
        session_destroy();
        header('Location: /login');
        break;
    default:
        http_response_code(404);
        echo "404 Not Found";
        break;
}
