<?php

namespace App\Services;

use App\Models\Hotel;
use Illuminate\Support\Facades\Auth;

class PackageService
{
    protected PackageModuleService $moduleService;

    public function __construct(PackageModuleService $moduleService)
    {
        $this->moduleService = $moduleService;
    }

    /**
     * Needed by DashboardController via HasPackageAccess trait
     */
    public function hasModuleAccess($hotel, string $moduleKey): bool
    {
        if (!$hotel) return false;
        return $this->moduleService->hasAccessToModule($hotel, $moduleKey);
    }
    
    /**
     * Needed by DashboardController via HasPackageAccess trait
     */
    public function getAvailableModules($hotel): array
    {
        if (!$hotel) return [];
        $packageLevel = $hotel->package_name ?? 'silver';
        return $this->moduleService->getAllowedModulesForPackage($packageLevel);
    }

    /**
     * Needed by DashboardController line 263
     */
    public function getPackageDetails(string $packageName): array
    {
        $packages = [
            'silver' => [
                'name' => 'Silver',
                'max_rooms' => 50,
                'max_users' => 10,
                'features' => ['Rooms', 'Reservations', 'Accounting', 'Front Desk']
            ],
            'gold' => [
                'name' => 'Gold',
                'max_rooms' => 150,
                'max_users' => 30,
                'features' => ['All Silver', 'Inventory', 'Housekeeping', 'Maintenance']
            ],
            'platinum' => [
                'name' => 'Platinum',
                'max_rooms' => 500,
                'max_users' => 100,
                'features' => ['All Gold', 'Food & Beverage', 'Services', 'HR']
            ],
            'custom' => [
                'name' => 'Custom',
                'max_rooms' => -1,
                'max_users' => -1,
                'features' => ['Custom Set']
            ]
        ];
        
        return $packages[strtolower($packageName)] ?? $packages['silver'];
    }

    /**
     * Backward compatibility with anything calling the old hasAccessToModule directly
     */
    public function hasAccessToModule(string $moduleCode): bool
    {
        $user = Auth::user();
        if (!$user || !$user->hotel) {
            return false;
        }
        return $this->moduleService->hasAccessToModule($user->hotel, $moduleCode);
    }
    
    public function getCurrentPackageId()
    {
        return session('current_package_id', 1);
    }
}