<?php
// app/Controllers/AuthController.php
require_once __DIR__.'/../../config/app.php';
require_once __DIR__.'/../Models/User.php';

class AuthController {

    public static function showLogin(): void {
        $u = currentUser();
        if ($u) self::redirectByRole($u['role']);
        require __DIR__.'/../Views/auth/login.php';
    }

    public static function login(): void {
        verifyCsrf();
        $email    = trim($_POST['email'] ?? '');
        $password = $_POST['password'] ?? '';
        $user = User::findByEmail($email);
        if (!$user || !password_verify($password, $user['password'])) {
            flash('auth', t('invalid_credentials'), 'danger');
            redirect(BASE_URL.'/index.php');
        }
        unset($user['password']);
        $_SESSION['user'] = $user;
        flash('dash', t('login_success'));
        self::redirectByRole($user['role']);
    }

    public static function register(): void {
        verifyCsrf();
        $name     = trim($_POST['name']     ?? '');
        $email    = trim($_POST['email']    ?? '');
        $phone    = trim($_POST['phone']    ?? '');
        $role     = $_POST['role']          ?? 'driver';
        $password = $_POST['password']      ?? '';
        $confirm  = $_POST['confirm']       ?? '';

        if (!$name || !$email || !$password) {
            flash('auth', t('fill_all_fields'), 'danger');
            redirect(BASE_URL.'/index.php?tab=register');
        }
        if ($password !== $confirm) {
            flash('auth', t('password_mismatch'), 'danger');
            redirect(BASE_URL.'/index.php?tab=register');
        }
        if (!in_array($role, ['owner','driver','officer'], true)) {
            $role = 'driver';
        }
        if (User::findByEmail($email)) {
            flash('auth', t('email_exists'), 'danger');
            redirect(BASE_URL.'/index.php?tab=register');
        }
        $id   = User::create(compact('name','email','phone','role','password'));
        $user = User::find($id);
        unset($user['password']);
        $_SESSION['user'] = $user;
        flash('dash', t('register_success'));
        self::redirectByRole($role);
    }

    public static function logout(): void {
        session_destroy();
        redirect(BASE_URL.'/index.php');
    }

    private static function redirectByRole(string $role): void {
        $map = [
            'owner'   => BASE_URL.'/owner/dashboard.php',
            'driver'  => BASE_URL.'/driver/dashboard.php',
            'officer' => BASE_URL.'/officer/dashboard.php',
            'admin'   => BASE_URL.'/admin/dashboard.php',
        ];
        redirect(isset($map[$role]) ? $map[$role] : BASE_URL.'/index.php');
    }
}

// ── Route (PHP 7.4 compatible — no match()) ───────────────
$action = isset($_GET['action'])  ? $_GET['action']  :
         (isset($_POST['action']) ? $_POST['action']  : 'login');

switch ($action) {
    case 'login':
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            AuthController::login();
        } else {
            AuthController::showLogin();
        }
        break;
    case 'register':
        AuthController::register();
        break;
    case 'logout':
        AuthController::logout();
        break;
    default:
        AuthController::showLogin();
        break;
}
