<?php

namespace App\Controllers;

use App\Models\User;
use App\Models\Student;
use App\Core\Auth;

class AuthController {
    public function login() {
        if (Auth::check()) {
            redirect('/dashboard');
            exit;
        }
        $error = null;
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $phone = $_POST['phone'] ?? '';
            $password = $_POST['password'] ?? '';

            $studentModel = new Student();
            $student = $studentModel->findByPhone($phone);

            if ($student && !empty($student['email'])) {
                if ($student['status'] !== 'Active') {
                    $error = "Account is frozen or inactive. Please contact support.";
                } else {
                    $userModel = new User();
                    $user = $userModel->findByEmail($student['email']);

                    if ($user && password_verify($password, $user['password'])) {
                        if ($user['role'] === 'student') {
                            Auth::login($user);
                            redirect('/dashboard');
                            exit;
                        } else {
                            $error = "Access denied. Use admin login.";
                        }
                    } else {
                        $error = "Invalid credentials.";
                    }
                }
            } else {
                $error = "Student record not found.";
            }
        }
        require_once __DIR__ . '/../../views/auth/login.php';
    }

    public function adminLogin() {
        if (Auth::check()) {
            redirect('/dashboard');
            exit;
        }
        $error = null;
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $email = $_POST['email'] ?? '';
            $password = $_POST['password'] ?? '';

            $userModel = new User();
            $user = $userModel->findByEmail($email);

            if ($user && password_verify($password, $user['password'])) {
                if ($user['role'] === 'admin') {
                    Auth::login($user);
                    redirect('/dashboard');
                    exit;
                } else {
                    $error = "Access denied. Use staff login for staff accounts.";
                }
            } else {
                $error = "Invalid credentials.";
            }
        }
        require_once __DIR__ . '/../../views/auth/admin_login.php';
    }

    public function staffLogin() {
        if (Auth::check()) {
            redirect('/dashboard');
            exit;
        }
        $error = null;
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $email = $_POST['email'] ?? '';
            $password = $_POST['password'] ?? '';

            $userModel = new User();
            $user = $userModel->findByEmail($email);

            if ($user && password_verify($password, $user['password'])) {
                if ($user['role'] === 'staff') {
                    Auth::login($user);
                    redirect('/staff/dashboard');
                    exit;
                } else {
                    $error = "Access denied. Use admin login for administrator accounts.";
                }
            } else {
                $error = "Invalid credentials.";
            }
        }
        require_once __DIR__ . '/../../views/auth/staff_login.php';
    }

    public function logout() {
        Auth::logout();
        redirect('/');
        exit;
    }
}
