<?php

namespace App\Core\Registry;

use Illuminate\Support\Collection;
use App\Modules\PLATFORM\Settings\Models\Company;
use App\Modules\PLATFORM\Settings\Models\Branch;

class ModuleRegistry
{
    protected static array $modules = [];
    protected static array $metadata = [];
    protected static ?Company $currentCompany = null;
    protected static ?Branch $currentBranch = null;

    /**
     * Register a module with metadata
     */
    public static function register(string $module, array $metadata = []): void
    {
        static::$modules[] = $module;
        static::$metadata[$module] = $metadata;
    }

    /**
     * Get all registered modules
     */
    public static function all(): array
    {
        return static::$modules;
    }

    /**
     * Get module metadata
     */
    public static function metadata(string $module): array
    {
        return static::$metadata[$module] ?? [];
    }

    /**
     * Set current company and branch context
     */
    public static function setContext(?Company $company, ?Branch $branch = null): void
    {
        static::$currentCompany = $company;
        static::$currentBranch = $branch;
    }

    /**
     * Check if module is enabled for current context
     * Checks both license (global) and company-level enablement
     */
    public static function enabled(string $module): bool
    {
        // Level 1: Check license (global permission)
       
        if (!in_array($module, LicenseRegistry::enabledModules())) {
            return false;
        }
        // Level 2: Check company-level enablement
        if (static::$currentCompany) {
            return static::$currentCompany->hasModule($module);
        }

        // If no company context, check only license
        return true;
    }

    /**
     * Get all enabled modules for current company
     */
    public static function enabledForCompany(?Company $company = null): Collection
    {
        $company = $company ?? static::$currentCompany;

        if (!$company) {
            return collect(LicenseRegistry::enabledModules());
        }

        // Get license-allowed modules
        $licenseModules = collect(LicenseRegistry::enabledModules());

        // Get company-enabled modules
        $companyModules = collect($company->enabled_modules ?? []);

        // Return intersection (modules that are both licensed AND enabled for company)
        return $licenseModules->intersect($companyModules);
    }

    /**
     * Check if module is registered
     */
    public static function exists(string $module): bool
    {
        return in_array($module, static::$modules);
    }

    /**
     * Get module dependencies
     */
    public static function dependencies(string $module): array
    {
        return static::$metadata[$module]['requires'] ?? [];
    }

    /**
     * Get module priority (for loading order)
     */
    public static function priority(string $module): int
    {
        return static::$metadata[$module]['priority'] ?? 100;
    }

    /**
     * Get current company
     */
    public static function getCurrentCompany(): ?Company
    {
        return static::$currentCompany;
    }

    /**
     * Get current branch
     */
    public static function getCurrentBranch(): ?Branch
    {
        return static::$currentBranch;
    }
}
