<?php
// app/Services/PermissionService.php

namespace App\Services;

use App\Models\User;
use Illuminate\Support\Facades\Gate;

class PermissionService
{
    /**
     * Define all permissions
     */
    const PERMISSIONS = [
        // Room Management
        'view_rooms',
        'create_rooms',
        'edit_rooms',
        'delete_rooms',
        'manage_room_status',
        
        // Reservations
        'view_reservations',
        'create_reservations',
        'edit_reservations',
        'delete_reservations',
        'cancel_reservations',
        'checkin_reservations',
        'checkout_reservations',
        
        // Guests
        'view_guests',
        'create_guests',
        'edit_guests',
        'delete_guests',
        'blacklist_guests',
        
        // Front Desk
        'view_frontdesk',
        'manage_frontdesk',
        'process_checkins',
        'process_checkouts',
        'walkin_registration',
        
        // Services
        'view_services',
        'book_services',
        'manage_services',
        'delete_services',
        
        // Housekeeping
        'view_housekeeping',
        'manage_housekeeping',
        'assign_tasks',
        'update_room_status',
        
        // Maintenance
        'view_maintenance',
        'create_maintenance_requests',
        'manage_maintenance',
        'assign_technicians',
        
        // Inventory
        'view_inventory',
        'manage_inventory',
        'adjust_stock',
        'create_purchase_orders',
        'approve_purchase_orders',
        
        // Accounts/Finance
        'view_financial',
        'manage_accounts',
        'process_payments',
        'create_invoices',
        'view_reports_financial',
        
        // Human Resources
        'view_hr',
        'manage_staff',
        'view_attendance',
        'manage_leaves',
        'view_payroll',
        'manage_payroll',
        
        // Documents
        'view_documents',
        'upload_documents',
        'manage_documents',
        'delete_documents',
        'share_documents',
        
        // Reports
        'view_reports',
        'generate_reports',
        'export_reports',
        'view_analytics',
        
        // Surveys
        'view_surveys',
        'respond_surveys',
        'create_surveys',
        'manage_surveys',
        
        // Settings
        'view_settings',
        'manage_settings',
        
        // Food & Beverage
        'view_fnb',
        'manage_menu',
        'process_orders',
        'manage_kitchen',
    ];

    /**
     * Get permissions for a specific role
     *
     * @param string $role
     * @return array
     */
    public static function getRolePermissions(string $role): array
    {
        $permissions = [
            'director' => self::getDirectorPermissions(),
            'manager' => self::getManagerPermissions(),
            'front_desk' => self::getFrontDeskPermissions(),
            'housekeeping' => self::getHousekeepingPermissions(),
            'food_beverage' => self::getFoodBeveragePermissions(),
            'maintenance' => self::getMaintenancePermissions(),
            'accountant' => self::getAccountantPermissions(),
        ];

        return $permissions[$role] ?? [];
    }

    /**
     * Director/General Manager - Full Access
     */
    private static function getDirectorPermissions(): array
    {
        return self::PERMISSIONS; // All permissions
    }

    /**
     * Manager/Department Heads - Operational Management
     */
    private static function getManagerPermissions(): array
    {
        return [
            // Room Management - Full Access
            'view_rooms', 'create_rooms', 'edit_rooms', 'delete_rooms', 'manage_room_status',
            
            // Reservations - Full Access
            'view_reservations', 'create_reservations', 'edit_reservations', 'delete_reservations',
            'cancel_reservations', 'checkin_reservations', 'checkout_reservations',
            
            // Guests - Full Access
            'view_guests', 'create_guests', 'edit_guests', 'delete_guests', 'blacklist_guests',
            
            // Front Desk - View Only
            'view_frontdesk',
            
            // Services - View Only
            'view_services',
            
            // Housekeeping - Full Access
            'view_housekeeping', 'manage_housekeeping', 'assign_tasks', 'update_room_status',
            
            // Maintenance - Full Access
            'view_maintenance', 'create_maintenance_requests', 'manage_maintenance', 'assign_technicians',
            
            // Inventory - Full Access
            'view_inventory', 'manage_inventory', 'adjust_stock', 'create_purchase_orders',
            
            // Accounts - View Only
            'view_financial', 'view_reports_financial',
            
            // Human Resources - View Only
            'view_hr', 'view_attendance', 'view_payroll',
            
            // Documents - Full Access
            'view_documents', 'upload_documents', 'manage_documents', 'share_documents',
            
            // Reports - Full Access
            'view_reports', 'generate_reports', 'export_reports', 'view_analytics',
            
            // Surveys - View & Respond
            'view_surveys', 'respond_surveys',
            
            // Settings - No Access (intentionally omitted)
            // Food & Beverage - No Access (intentionally omitted)
        ];
    }

    /**
     * Front Desk Staff Permissions
     */
    private static function getFrontDeskPermissions(): array
    {
        return [
            // Reservations - Create, View, Modify
            'view_reservations', 'create_reservations', 'edit_reservations',
            
            // Front Desk - Full Access
            'view_frontdesk', 'manage_frontdesk', 'process_checkins', 'process_checkouts', 'walkin_registration',
            
            // Services - View & Book
            'view_services', 'book_services',
            
            // Housekeeping - View Only
            'view_housekeeping',
            
            // Maintenance - Create Requests
            'create_maintenance_requests',
            
            // Human Resource - View Own Schedule
            'view_attendance',
        ];
    }

    /**
     * Housekeeping Staff Permissions
     */
    private static function getHousekeepingPermissions(): array
    {
        return [
            // Housekeeping - Full Access
            'view_housekeeping', 'manage_housekeeping', 'assign_tasks', 'update_room_status',
            
            // Maintenance - Full Access
            'view_maintenance', 'create_maintenance_requests', 'manage_maintenance',
        ];
    }

    /**
     * Food & Beverage Staff Permissions
     */
    private static function getFoodBeveragePermissions(): array
    {
        return [
            // Food & Beverage - Full Access
            'view_fnb', 'manage_menu', 'process_orders', 'manage_kitchen',
            
            // Maintenance - Report Issues
            'create_maintenance_requests',
        ];
    }

    /**
     * Maintenance Staff Permissions
     */
    private static function getMaintenancePermissions(): array
    {
        return [
            // Maintenance - Full Access
            'view_maintenance', 'create_maintenance_requests', 'manage_maintenance', 'assign_technicians',
            
            // Housekeeping - View Only (for room status)
            'view_housekeeping',
        ];
    }

    /**
     * Accountant/Finance Staff Permissions
     */
    private static function getAccountantPermissions(): array
    {
        return [
            // Accounts/Finance - Full Access
            'view_financial', 'manage_accounts', 'process_payments', 'create_invoices', 'view_reports_financial',
            
            // Reports - Financial Reports Only
            'view_reports', 'generate_reports', 'export_reports',
            
            // Human Resources - View Payroll
            'view_payroll',
            
            // Inventory - View Only (for costing)
            'view_inventory',
            
            // Documents - View Financial Documents
            'view_documents',
        ];
    }

    /**
     * Check if user has permission
     *
     * @param User $user
     * @param string $permission
     * @return bool
     */
    public static function hasPermission(User $user, string $permission): bool
    {
        $rolePermissions = self::getRolePermissions($user->role);
        return in_array($permission, $rolePermissions);
    }
}