<?php
declare(strict_types=1);

namespace ROITheme\Admin\CustomCSSManager\Application\UseCases;

use ROITheme\Admin\CustomCSSManager\Application\DTOs\SaveSnippetRequest;
use ROITheme\Admin\CustomCSSManager\Domain\Entities\CSSSnippet;
use ROITheme\Shared\Domain\Contracts\CSSSnippetRepositoryInterface;

/**
 * Caso de uso: Guardar snippet CSS
 *
 * SRP: Solo responsable de orquestar el guardado
 */
final class SaveSnippetUseCase
{
    public function __construct(
        private readonly CSSSnippetRepositoryInterface $repository
    ) {}

    public function execute(SaveSnippetRequest $request): void
    {
        // 1. Crear entidad desde DTO
        $snippet = CSSSnippet::fromArray($request->toArray());

        // 2. Validar en dominio
        $snippet->validate();

        // 3. Validar tamaño según tipo
        $snippet->css()->validateForLoadType($snippet->loadType());

        // 4. Persistir
        $this->repository->save($snippet->toArray());
    }
}
