<?php
declare(strict_types=1);

namespace ROITheme\Shared\Infrastructure\Services;

use ROITheme\Shared\Domain\Contracts\PageVisibilityRepositoryInterface;
use ROITheme\Shared\Domain\Constants\VisibilityDefaults;

/**
 * Servicio para migrar configuración de visibilidad inicial
 *
 * @package ROITheme\Shared\Infrastructure\Services
 */
final class MigratePageVisibilityService
{
    // NOTA: Usa VisibilityDefaults::DEFAULT_VISIBILITY para cumplir DRY

    public function __construct(
        private readonly PageVisibilityRepositoryInterface $visibilityRepository
    ) {}

    /**
     * Ejecuta la migración para todos los componentes
     *
     * @return array{created: int, skipped: int}
     */
    public function migrate(): array
    {
        $created = 0;
        $skipped = 0;

        $components = $this->visibilityRepository->getAllComponentNames();

        foreach ($components as $componentName) {
            if ($this->visibilityRepository->hasVisibilityConfig($componentName)) {
                $skipped++;
                continue;
            }

            // Usar constante compartida (DRY)
            $this->visibilityRepository->createDefaultVisibility(
                $componentName,
                VisibilityDefaults::DEFAULT_VISIBILITY
            );
            $created++;
        }

        return [
            'created' => $created,
            'skipped' => $skipped,
        ];
    }
}
