<?php

class Auth {
    /**
     * Check if user is logged in
     */
    public static function check(): bool {
        return Session::has('user_id');
    }

    /**
     * Get logged-in user array
     */
    public static function user(): ?array {
        if (!self::check()) {
            return null;
        }

        static $cachedUser = null;
        if ($cachedUser === null) {
            $userModel = new User();
            $cachedUser = $userModel->findById(Session::get('user_id'));
        }
        return $cachedUser;
    }

    /**
     * Get seller profile array if user is an approved/pending seller
     */
    public static function sellerProfile(): ?array {
        $user = self::user();
        if (!$user) return null;

        static $cachedSeller = null;
        if ($cachedSeller === null) {
            $sellerModel = new SellerProfile();
            $cachedSeller = $sellerModel->findOneWhere(['user_id' => $user['id']]);
        }
        return $cachedSeller;
    }

    /**
     * Check if current user is Admin
     */
    public static function isAdmin(): bool {
        $user = self::user();
        return $user && $user['role'] === 'admin';
    }

    /**
     * Check if current user is an Approved Seller
     */
    public static function isApprovedSeller(): bool {
        $seller = self::sellerProfile();
        return $seller && $seller['status'] === 'approved' && empty($seller['deleted_at']);
    }

    /**
     * Middleware: Require logged in user
     */
    public static function requireAuth(): void {
        if (!self::check()) {
            Session::setFlash('warning', 'กรุณาเข้าสู่ระบบก่อนเข้าใช้งาน');
            header('Location: ' . APP_URL . '/login');
            exit;
        }
    }

    /**
     * Middleware: Require Admin
     */
    public static function requireAdmin(): void {
        self::requireAuth();
        if (!self::isAdmin()) {
            Session::setFlash('danger', 'การเข้าถึงถูกปฏิเสธ จำเป็นต้องมีสิทธิ์ผู้ดูแลระบบ (Admin)');
            header('Location: ' . APP_URL . '/');
            exit;
        }
    }

    /**
     * Middleware: Require Approved Seller
     */
    public static function requireSeller(): void {
        self::requireAuth();
        $seller = self::sellerProfile();

        if (!$seller || !empty($seller['deleted_at'])) {
            Session::setFlash('danger', 'ไม่พบข้อมูลร้านค้าของคุณ หรือร้านค้าถูกลบออกจากระบบแล้ว');
            header('Location: ' . APP_URL . '/seller/apply');
            exit;
        }

        if ($seller['status'] === 'suspended') {
            Session::setFlash('warning', 'ร้านค้าของคุณถูกระงับการใช้งานชั่วคราว กรุณาติดต่อผู้ดูแลระบบ');
            header('Location: ' . APP_URL . '/seller/apply');
            exit;
        }

        if ($seller['status'] === 'banned') {
            Session::setFlash('danger', 'ร้านค้าของคุณถูกแบนเนื่องจากทำผิดกฎของแพลตฟอร์ม');
            header('Location: ' . APP_URL . '/seller/apply');
            exit;
        }

        if ($seller['status'] !== 'approved') {
            Session::setFlash('warning', 'ร้านค้าของคุณยังไม่ได้รับการอนุมัติ');
            header('Location: ' . APP_URL . '/seller/apply');
            exit;
        }
    }
}
