<?php

namespace App\Services;

use App\Models\Hotel;

class PackageModuleService
{
    protected array $modulePackageMap = [
        // Silver Package Modules
        'rooms' => 'silver',
        'reservations' => 'silver',
        'front_desk' => 'silver',
        'accounting' => 'silver',
        'settings' => 'silver',
        
        // Gold Package Additions
        'inventory' => 'gold',
        'housekeeping' => 'gold',
        'maintenance' => 'gold',
        'reporting' => 'gold',
        
        // Platinum Package Additions
        'food_beverage' => 'platinum',
        'hotel_services' => 'platinum',
        'hr' => 'platinum',
        'survey' => 'platinum',
        'documents' => 'platinum',
    ];

    protected array $moduleSourceMapping = [
        'pos' => 'food_beverage',
        'service' => 'hotel_services',
        'inventory' => 'inventory',
        'hr' => 'hr',
        'housekeeping' => 'housekeeping',
        'maintenance' => 'maintenance',
        'room_charge' => 'rooms',
        'payment' => 'front_desk',
        'reservation' => 'reservations',
    ];

    /**
     * Get all allowed modules for a given package
     */
    public function getAllowedModulesForPackage(string $packageLevel): array
    {
        $allowed = [];
        $levels = ['silver' => 1, 'gold' => 2, 'platinum' => 3];
        $targetLevel = $levels[strtolower($packageLevel)] ?? 0;

        foreach ($this->modulePackageMap as $module => $reqPackage) {
            $reqLevel = $levels[$reqPackage] ?? 99;
            if ($targetLevel >= $reqLevel) {
                $allowed[] = $module;
            }
        }
        return $allowed;
    }

    /**
     * Get all allowed module_sources (for journal entries) base on package level
     */
    public function getAllowedSourcesForPackage(string $packageLevel): array
    {
        $allowedModules = $this->getAllowedModulesForPackage($packageLevel);
        
        $allowedSources = [];
        foreach ($this->moduleSourceMapping as $source => $module) {
            if (in_array($module, $allowedModules)) {
                $allowedSources[] = $source;
            }
        }
        
        // Standard sources that are always allowed if accounting is enabled
        if (in_array('accounting', $allowedModules)) {
            $allowedSources = array_merge($allowedSources, ['manual', 'system', 'journal_voucher', 'payment_voucher', 'receipt']);
        }
        
        return array_unique($allowedSources);
    }

    /**
     * Check if a hotel has access to a specific module
     */
    public function hasAccessToModule(Hotel $hotel, string $module): bool
    {
        // First check JSON enabled_modules if present
        if (!empty($hotel->enabled_modules)) {
            $enabledModules = is_string($hotel->enabled_modules) ? json_decode($hotel->enabled_modules, true) : $hotel->enabled_modules;
            if (is_array($enabledModules) && in_array($module, $enabledModules)) {
                return true;
            }
        }

        // Fallback to active subscription package validation
        // Default to 'silver' if no logical mapping exists to ensure fallback safe restriction
        $packageLevel = $hotel->package_name ?? 'silver'; 
        
        $allowed = $this->getAllowedModulesForPackage($packageLevel);
        return in_array($module, $allowed);
    }
}
