<?php
declare(strict_types=1);

namespace ROITheme\Shared\Application\UseCases\SyncSchema;

/**
 * SyncSchemaResponse - DTO de salida para sincronizar schemas
 *
 * RESPONSABILIDAD: Encapsular resultado de sincronización
 *
 * DATOS INCLUIDOS:
 * - success: bool
 * - componentsAdded: array de nombres agregados
 * - componentsUpdated: array de nombres actualizados
 * - componentsDeleted: array de nombres eliminados
 * - errors: array de errores
 *
 * @package ROITheme\Shared\Application\UseCases\SyncSchema
 */
final readonly class SyncSchemaResponse
{
    /**
     * @param bool $success Indica si la sincronización fue exitosa
     * @param array $componentsAdded Componentes agregados
     * @param array $componentsUpdated Componentes actualizados
     * @param array $componentsDeleted Componentes eliminados
     * @param array $errors Errores ocurridos
     */
    private function __construct(
        private bool $success,
        private array $componentsAdded,
        private array $componentsUpdated,
        private array $componentsDeleted,
        private array $errors
    ) {}

    public function isSuccess(): bool
    {
        return $this->success;
    }

    public function getComponentsAdded(): array
    {
        return $this->componentsAdded;
    }

    public function getComponentsUpdated(): array
    {
        return $this->componentsUpdated;
    }

    public function getComponentsDeleted(): array
    {
        return $this->componentsDeleted;
    }

    public function getErrors(): array
    {
        return $this->errors;
    }

    /**
     * Factory method: Sincronización exitosa
     */
    public static function success(
        array $added,
        array $updated,
        array $deleted
    ): self {
        return new self(true, $added, $updated, $deleted, []);
    }

    /**
     * Factory method: Sincronización con errores
     */
    public static function failure(array $errors): self
    {
        return new self(false, [], [], [], $errors);
    }

    /**
     * Convertir a array
     */
    public function toArray(): array
    {
        return [
            'success' => $this->success,
            'components_added' => $this->componentsAdded,
            'components_updated' => $this->componentsUpdated,
            'components_deleted' => $this->componentsDeleted,
            'errors' => $this->errors
        ];
    }

    /**
     * Obtener resumen de cambios
     */
    public function getSummary(): string
    {
        $added = count($this->componentsAdded);
        $updated = count($this->componentsUpdated);
        $deleted = count($this->componentsDeleted);

        return sprintf(
            'Added: %d, Updated: %d, Deleted: %d',
            $added,
            $updated,
            $deleted
        );
    }
}
