<?php
// app/Controllers/AuthController.php

class AuthController {
    public function showLogin() {
        if (isset($_SESSION['user_id'])) {
            $this->redirect($_SESSION['role']);
        }
        require_once 'app/Views/auth/login.php';
    }

    public function login() {
        $email    = $_POST['email'] ?? '';
        $password = $_POST['password'] ?? '';

        $db   = getDB();
        $stmt = $db->prepare('SELECT * FROM users WHERE email = ? LIMIT 1');
        $stmt->execute([$email]);
        $user = $stmt->fetch();

        if ($user && password_verify($password, $user['password'])) {
            if ($user['status'] !== 'active') {
                $_SESSION['error'] = 'Account is not active.';
                header('Location: /restpo/login'); exit;
            }

            // Enforce staff limits on login
            if ($user['restaurant_id'] && !FeatureGate::isUserAllowed($user['id'], $user['restaurant_id'], $user['role'])) {
                $_SESSION['error'] = 'Login suspended. This user account exceeds your subscription package limits. Please contact your restaurant administrator.';
                header('Location: /restpo/login'); exit;
            }

            $_SESSION['user_id']       = $user['id'];
            $_SESSION['role']          = $user['role'];
            $_SESSION['restaurant_id'] = $user['restaurant_id'];
            $_SESSION['name']          = $user['name'];
            $_SESSION['email']         = $user['email'];
            $_SESSION['phone']         = $user['phone'] ?? '';
            $_SESSION['currency']      = $user['currency'] ?? '$';
            $_SESSION['logo']          = $user['logo'] ?? '';

            if ($user['restaurant_id']) {
                $rStmt = $db->prepare('SELECT name, status, currency FROM restaurants WHERE id = ?');
                $rStmt->execute([$user['restaurant_id']]);
                $restaurant = $rStmt->fetch();

                if (!$restaurant || $restaurant['status'] !== 'active') {
                    $_SESSION['error'] = 'Restaurant account is suspended or inactive.';
                    header('Location: /restpo/login'); exit;
                }
                $_SESSION['restaurant_name'] = $restaurant['name'];
                $_SESSION['currency']        = $restaurant['currency'] ?? '$';
            }

            $this->redirect($user['role']);
        } else {
            $_SESSION['error'] = 'Invalid email or password.';
            header('Location: /restpo/login'); exit;
        }
    }

    private function redirect($role) {
        $map = [
            'superadmin' => '/restpo/superadmin/dashboard',
            'admin'      => '/restpo/admin/dashboard',
            'cashier'    => '/restpo/pos',
            'waiter'     => '/restpo/waiter',
            'kitchen'    => '/restpo/kitchen',
            'inventory'  => '/restpo/inventory',
            'hr_manager' => '/restpo/hr',
        ];
        header('Location: ' . ($map[$role] ?? '/restpo/login'));
        exit;
    }

    public function logout() {
        session_destroy();
        header('Location: /restpo/login'); exit;
    }
}
