<?php

namespace MintyPHP\Service\Access;

use MintyPHP\Service\Tenant\TenantScopeService;

class AccessPolicyFactory
{
    private ?UserAuthorizationPolicy $userAuthorizationPolicy = null;
    private ?RoleAuthorizationPolicy $roleAuthorizationPolicy = null;
    private ?PermissionAuthorizationPolicy $permissionAuthorizationPolicy = null;
    private ?OperationsAuthorizationPolicy $operationsAuthorizationPolicy = null;
    private ?SettingsAuthorizationPolicy $settingsAuthorizationPolicy = null;
    private ?TenantAuthorizationPolicy $tenantAuthorizationPolicy = null;
    private ?DepartmentAuthorizationPolicy $departmentAuthorizationPolicy = null;
    private ?AuthorizationService $authorizationService = null;

    public function __construct(
        private readonly PermissionService $permissionService,
        private readonly TenantScopeService $tenantScopeService
    ) {
    }

    public function createUserAuthorizationPolicy(): UserAuthorizationPolicy
    {
        return $this->userAuthorizationPolicy ??= new UserAuthorizationPolicy(
            $this->permissionService,
            $this->tenantScopeService
        );
    }

    public function createRoleAuthorizationPolicy(): RoleAuthorizationPolicy
    {
        return $this->roleAuthorizationPolicy ??= new RoleAuthorizationPolicy($this->permissionService);
    }

    public function createPermissionAuthorizationPolicy(): PermissionAuthorizationPolicy
    {
        return $this->permissionAuthorizationPolicy ??= new PermissionAuthorizationPolicy($this->permissionService);
    }

    public function createOperationsAuthorizationPolicy(): OperationsAuthorizationPolicy
    {
        return $this->operationsAuthorizationPolicy ??= new OperationsAuthorizationPolicy($this->permissionService);
    }

    public function createSettingsAuthorizationPolicy(): SettingsAuthorizationPolicy
    {
        return $this->settingsAuthorizationPolicy ??= new SettingsAuthorizationPolicy($this->permissionService);
    }

    public function createTenantAuthorizationPolicy(): TenantAuthorizationPolicy
    {
        return $this->tenantAuthorizationPolicy ??= new TenantAuthorizationPolicy(
            $this->permissionService,
            $this->tenantScopeService
        );
    }

    public function createDepartmentAuthorizationPolicy(): DepartmentAuthorizationPolicy
    {
        return $this->departmentAuthorizationPolicy ??= new DepartmentAuthorizationPolicy(
            $this->permissionService,
            $this->tenantScopeService
        );
    }

    // Assembles all policies into a single AuthorizationService.
    // Policy order doesn't matter — each ability maps to exactly one policy via supports().
    public function createAuthorizationService(): AuthorizationService
    {
        return $this->authorizationService ??= new AuthorizationService([
            $this->createUserAuthorizationPolicy(),
            $this->createRoleAuthorizationPolicy(),
            $this->createPermissionAuthorizationPolicy(),
            $this->createOperationsAuthorizationPolicy(),
            $this->createSettingsAuthorizationPolicy(),
            $this->createTenantAuthorizationPolicy(),
            $this->createDepartmentAuthorizationPolicy(),
        ]);
    }
}
