<?php

namespace MintyPHP\Service\Audit;

use MintyPHP\Http\RequestRuntimeInterface;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Repository\Audit\ApiAuditLogRepositoryInterface;
use MintyPHP\Repository\Audit\SystemAuditLogRepositoryInterface;
use MintyPHP\Repository\Audit\UserLifecycleAuditRepositoryInterface;
use MintyPHP\Service\Settings\SettingsSystemAuditGateway;

class AuditServicesFactory
{
    private ?ApiAuditService $apiAuditService = null;
    private ?UserLifecycleAuditService $userLifecycleAuditService = null;
    private ?SystemAuditRedactionService $systemAuditRedactionService = null;
    private ?SystemAuditService $systemAuditService = null;

    public function __construct(
        private readonly AuditRepositoryFactory $auditRepositoryFactory,
        private readonly SettingsSystemAuditGateway $settingsSystemAuditGateway,
        private readonly RequestRuntimeInterface $requestRuntime,
        private readonly SessionStoreInterface $sessionStore
    ) {
    }

    public function createApiAuditLogRepository(): ApiAuditLogRepositoryInterface
    {
        return $this->auditRepositoryFactory->createApiAuditLogRepository();
    }

    public function createUserLifecycleAuditRepository(): UserLifecycleAuditRepositoryInterface
    {
        return $this->auditRepositoryFactory->createUserLifecycleAuditRepository();
    }

    public function createSystemAuditLogRepository(): SystemAuditLogRepositoryInterface
    {
        return $this->auditRepositoryFactory->createSystemAuditLogRepository();
    }

    public function createApiAuditService(): ApiAuditService
    {
        return $this->apiAuditService ??= new ApiAuditService(
            $this->createApiAuditLogRepository(),
            $this->requestRuntime
        );
    }

    public function createUserLifecycleAuditService(): UserLifecycleAuditService
    {
        return $this->userLifecycleAuditService ??= new UserLifecycleAuditService(
            $this->createUserLifecycleAuditRepository()
        );
    }

    public function createSystemAuditRedactionService(): SystemAuditRedactionService
    {
        return $this->systemAuditRedactionService ??= new SystemAuditRedactionService();
    }

    public function createSystemAuditService(): SystemAuditService
    {
        return $this->systemAuditService ??= new SystemAuditService(
            $this->createSystemAuditLogRepository(),
            $this->createSystemAuditRedactionService(),
            $this->settingsSystemAuditGateway,
            $this->sessionStore
        );
    }
}
