<?php

namespace App\Services\Configurator;

/**
 * Accumulates the per-model outcome of a configurator copy so the command can
 * present a summary. Purely a value collector - it performs no work itself.
 */
class ConfiguratorCopyReport
{
    public const STATUS_COPIED = 'copied';
    public const STATUS_REPLACED = 'replaced';
    public const STATUS_SKIPPED = 'skipped';
    public const STATUS_FAILED = 'failed';

    /** @var array<int, array{name: string, status: string, message: ?string, counts: array<string, int>}> */
    private array $models = [];

    /** @var string[] non-fatal warnings gathered across the whole run */
    private array $warnings = [];

    public function addModel(string $name, string $status, ?string $message = null, array $counts = []): void
    {
        $this->models[] = [
            'name' => $name,
            'status' => $status,
            'message' => $message,
            'counts' => $counts,
        ];
    }

    public function addWarning(string $message): void
    {
        $this->warnings[] = $message;
    }

    /**
     * @return array<int, array{name: string, status: string, message: ?string, counts: array<string, int>}>
     */
    public function models(): array
    {
        return $this->models;
    }

    /**
     * @return string[]
     */
    public function warnings(): array
    {
        return $this->warnings;
    }

    public function countByStatus(string $status): int
    {
        return count(array_filter($this->models, fn ($model) => $model['status'] === $status));
    }

    public function hasFailures(): bool
    {
        return $this->countByStatus(self::STATUS_FAILED) > 0;
    }
}
