<?php
declare(strict_types=1);

namespace ROITheme\Shared\Infrastructure\Facades;

use ROITheme\Shared\Application\UseCases\SaveComponent\SaveComponentUseCase;
use ROITheme\Shared\Application\UseCases\SaveComponent\SaveComponentRequest;
use ROITheme\Shared\Application\UseCases\GetComponent\GetComponentUseCase;
use ROITheme\Shared\Application\UseCases\GetComponent\GetComponentRequest;
use ROITheme\Shared\Application\UseCases\DeleteComponent\DeleteComponentUseCase;
use ROITheme\Shared\Application\UseCases\DeleteComponent\DeleteComponentRequest;
use ROITheme\Shared\Infrastructure\Di\DIContainer;

/**
 * ComponentManager - Facade para el sistema
 *
 * RESPONSABILIDAD: Punto de entrada unificado y simple
 *
 * PATRÓN: Facade
 * - Oculta complejidad interna
 * - Proporciona API simple
 * - Orquesta Use Cases
 *
 * USO:
 * ```php
 * $manager = new ComponentManager($container);
 * $result = $manager->saveComponent('top_bar', $data);
 * ```
 *
 * @package ROITheme\Infrastructure\Facades
 */
final class ComponentManager
{
    public function __construct(
        private DIContainer $container
    ) {}

    /**
     * Guardar componente
     *
     * @param string $componentName
     * @param array $data
     * @return array ['success' => bool, 'data' => mixed, 'errors' => array|null]
     */
    public function saveComponent(string $componentName, array $data): array
    {
        $request = new SaveComponentRequest($componentName, $data);

        $useCase = new SaveComponentUseCase(
            $this->container->getComponentRepository(),
            $this->container->getValidationService(),
            $this->container->getCacheService()
        );

        $response = $useCase->execute($request);

        return $response->toArray();
    }

    /**
     * Obtener componente
     *
     * @param string $componentName
     * @return array ['success' => bool, 'data' => mixed, 'error' => string|null]
     */
    public function getComponent(string $componentName): array
    {
        $request = new GetComponentRequest($componentName);

        $useCase = new GetComponentUseCase(
            $this->container->getComponentRepository(),
            $this->container->getCacheService()
        );

        $response = $useCase->execute($request);

        return $response->toArray();
    }

    /**
     * Eliminar componente
     *
     * @param string $componentName
     * @return array ['success' => bool, 'message' => string|null, 'error' => string|null]
     */
    public function deleteComponent(string $componentName): array
    {
        $request = new DeleteComponentRequest($componentName);

        $useCase = new DeleteComponentUseCase(
            $this->container->getComponentRepository(),
            $this->container->getCacheService()
        );

        $response = $useCase->execute($request);

        return [
            'success' => $response->isSuccess(),
            'message' => $response->getMessage(),
            'error' => $response->getError()
        ];
    }

    /**
     * Sincronizar schemas desde JSON
     *
     * @param string|null $componentName Si null, sincroniza todos
     * @return array
     */
    public function syncSchema(?string $componentName = null): array
    {
        $syncService = $this->container->getSchemaSyncService();

        if ($componentName === null) {
            return $syncService->syncAll();
        }

        return $syncService->syncComponent($componentName);
    }

    /**
     * Limpiar componentes obsoletos
     *
     * @return array ['removed' => array]
     */
    public function cleanup(): array
    {
        $cleanupService = $this->container->getCleanupService();
        return $cleanupService->removeObsolete();
    }

    /**
     * Invalidar todo el cache
     *
     * @return bool
     */
    public function invalidateCache(): bool
    {
        return $this->container->getCacheService()->invalidateAll();
    }
}
