<?php
error_reporting(0);
require_once __DIR__ . '/../models/User.php';

class AuthController {
    public function index($f3) {
        if (isset($_SESSION['user'])) {
            $role = $_SESSION['user']['role'];
            switch ($role) {
                case 'super_admin':
                    $f3->reroute('/super-admin');
                    break;
                case 'company_admin':
                    $f3->reroute('/company');
                    break;
                case 'staff':
                    $f3->reroute('/staff');
                    break;
                default:
                    $f3->reroute('/customer-portal');
            }
        }
        $f3->reroute('/login');
    }
    
    public function loginPage($f3) {
        if (isset($_SESSION['user'])) {
            $this->index($f3);
            return;
        }
        echo \Template::instance()->render('auth/login.html');
    }
    
    public function login($f3) {
        header('Content-Type: application/json');
        
        try {
            $data = json_decode(file_get_contents('php://input'), true);
            $email = $data['email'] ?? '';
            $password = $data['password'] ?? '';
            
            if (empty($email) || empty($password)) {
                http_response_code(400);
                echo json_encode(['success' => false, 'message' => 'Email and password are required']);
                return;
            }
            
            $db = $f3->get('DB');
            if (!$db) {
                http_response_code(500);
                echo json_encode(['success' => false, 'message' => 'Database connection error']);
                return;
            }
            
            $userModel = new \Models\User($db);
            
            $user = $userModel->findByEmail($email);
            
            if (!$user) {
                http_response_code(401);
                echo json_encode(['success' => false, 'message' => 'Invalid credentials']);
                return;
            }
            
            if (!$userModel->verifyPassword($password, $user['password'])) {
                http_response_code(401);
                echo json_encode(['success' => false, 'message' => 'Invalid credentials']);
                return;
            }
            
            if ($user['is_active'] != true) {
                http_response_code(403);
                echo json_encode(['success' => false, 'message' => 'Account is disabled']);
                return;
            }
            
            $_SESSION['user'] = [
                'id' => $user['id'],
                'email' => $user['email'],
                'name' => $user['name'],
                'role' => $user['role_name'],
                'role_id' => $user['role_id'],
                'company_id' => $user['company_id']
            ];
            
            $redirect = '/login';
            switch ($user['role_name']) {
                case 'super_admin':
                    $redirect = '/super-admin';
                    break;
                case 'company_admin':
                    $redirect = '/company';
                    break;
                case 'staff':
                    $redirect = '/staff';
                    break;
            }
            
            echo json_encode(['success' => true, 'redirect' => $redirect]);
        } catch (Exception $e) {
            http_response_code(500);
            echo json_encode(['success' => false, 'message' => 'Server error: ' . $e->getMessage()]);
        }
    }
    
    public function logout($f3) {
        session_destroy();
        $f3->reroute('/login');
    }
    
    public function forgotPassword($f3) {
        echo \Template::instance()->render('auth/forgot-password.html');
    }
}
