<?php

namespace MintyPHP\Module\Security;

use MintyPHP\App\AppContainer;
use MintyPHP\App\Container\ContainerRegistrar;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Module\Security\Repository\SecurityCheckRepository;
use MintyPHP\Module\Security\Repository\SecurityCheckTemplateRepository;
use MintyPHP\Module\Security\Repository\SecurityTokenRepository;
use MintyPHP\Module\Security\Service\SecurityBcGateway;
use MintyPHP\Module\Security\Service\SecurityBcSettingsGateway;
use MintyPHP\Module\Security\Service\SecurityCheckProcess;
use MintyPHP\Module\Security\Service\SecurityCheckService;
use MintyPHP\Module\Security\Service\SecurityCheckTemplateService;
use MintyPHP\Module\Security\Service\SecurityMarkdownGateway;
use MintyPHP\Module\Security\Service\SecurityOAuthTokenService;
use MintyPHP\Module\Security\Service\SecurityReportLogoService;
use MintyPHP\Module\Security\Service\SecurityReportPdfService;
use MintyPHP\Module\Security\Service\SecurityReportSettingsGateway;
use MintyPHP\Module\Security\Service\SecurityReportTemplateService;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\Branding\BrandingLogoService;
use MintyPHP\Service\Settings\SettingServicesFactory;
use MintyPHP\Service\Settings\SettingsMetadataGateway;
use MintyPHP\Service\Tenant\TenantLogoService;

final class SecurityContainerRegistrar implements ContainerRegistrar
{
    public function register(AppContainer $container): void
    {
        $container->set(SecurityAuthorizationPolicy::class, static fn (AppContainer $c): SecurityAuthorizationPolicy => new SecurityAuthorizationPolicy(
            $c->get(PermissionService::class)
        ));

        // --- Thin BC layer ---
        $container->set(SecurityBcSettingsGateway::class, static fn (AppContainer $c): SecurityBcSettingsGateway => new SecurityBcSettingsGateway(
            $c->get(SettingsMetadataGateway::class),
            $c->get(SettingServicesFactory::class)->createSettingsCryptoGateway()
        ));

        $container->set(SecurityTokenRepository::class, static fn (AppContainer $c): SecurityTokenRepository => new SecurityTokenRepository(
            $c->get(SettingServicesFactory::class)->createSettingsCryptoGateway()
        ));

        $container->set(SecurityOAuthTokenService::class, static fn (AppContainer $c): SecurityOAuthTokenService => new SecurityOAuthTokenService(
            $c->get(SecurityBcSettingsGateway::class),
            $c->get(SecurityTokenRepository::class)
        ));

        $container->set(SecurityBcGateway::class, static fn (AppContainer $c): SecurityBcGateway => new SecurityBcGateway(
            $c->get(SecurityBcSettingsGateway::class),
            $c->get(SecurityOAuthTokenService::class),
            $c->get(SessionStoreInterface::class)
        ));

        // --- Check templates (per-product checklist catalog) ---
        $container->set(SecurityCheckTemplateRepository::class, static fn (): SecurityCheckTemplateRepository => new SecurityCheckTemplateRepository());

        $container->set(SecurityCheckTemplateService::class, static fn (AppContainer $c): SecurityCheckTemplateService => new SecurityCheckTemplateService(
            $c->get(SecurityCheckTemplateRepository::class)
        ));

        $container->set(SecurityMarkdownGateway::class, static fn (): SecurityMarkdownGateway => new SecurityMarkdownGateway());

        // --- Security checks (instances) ---
        $container->set(SecurityCheckProcess::class, static fn (): SecurityCheckProcess => new SecurityCheckProcess());

        $container->set(SecurityCheckRepository::class, static fn (): SecurityCheckRepository => new SecurityCheckRepository());

        $container->set(SecurityCheckService::class, static fn (AppContainer $c): SecurityCheckService => new SecurityCheckService(
            $c->get(SecurityCheckRepository::class),
            $c->get(SecurityCheckTemplateService::class),
            $c->get(SecurityCheckProcess::class)
        ));

        // --- Customer report presentation (logo + HTML/CSS template) ---
        $container->set(SecurityReportSettingsGateway::class, static fn (AppContainer $c): SecurityReportSettingsGateway => new SecurityReportSettingsGateway(
            $c->get(SettingsMetadataGateway::class)
        ));

        $container->set(SecurityReportTemplateService::class, static fn (): SecurityReportTemplateService => new SecurityReportTemplateService());

        $container->set(SecurityReportLogoService::class, static fn (): SecurityReportLogoService => new SecurityReportLogoService());

        $container->set(SecurityReportPdfService::class, static fn (AppContainer $c): SecurityReportPdfService => new SecurityReportPdfService(
            $c->get(SecurityCheckProcess::class),
            $c->get(BrandingLogoService::class),
            $c->get(TenantLogoService::class),
            $c->get(SecurityReportSettingsGateway::class),
            $c->get(SecurityReportTemplateService::class),
            $c->get(SecurityReportLogoService::class)
        ));
    }
}
