<?php

declare(strict_types=1);

namespace App\Scolarite\Application\Port;

use function array_map;
use function count;

/**
 * Résultat de la vérification des règles de devoirs.
 *
 * Transporté à travers la frontière Scolarite ← Administration,
 * indépendant des types internes d'Administration.
 */
final readonly class HomeworkRulesCheckResult
{
    /**
     * @param RuleWarning[] $warnings
     * @param string[]      $suggestedDates Dates conformes alternatives (format Y-m-d)
     */
    public function __construct(
        public array $warnings,
        public bool $bloquant,
        public array $suggestedDates = [],
    ) {
    }

    public function estValide(): bool
    {
        return count($this->warnings) === 0;
    }

    public function estAvertissement(): bool
    {
        return !$this->estValide() && !$this->bloquant;
    }

    public function estBloquant(): bool
    {
        return !$this->estValide() && $this->bloquant;
    }

    /**
     * @return string[]
     */
    public function messages(): array
    {
        return array_map(
            static fn (RuleWarning $w): string => $w->message,
            $this->warnings,
        );
    }

    /**
     * @return string[]
     */
    public function ruleTypes(): array
    {
        return array_map(
            static fn (RuleWarning $w): string => $w->ruleType,
            $this->warnings,
        );
    }

    /**
     * @return array<array{ruleType: string, message: string, params: array<string, mixed>}>
     */
    public function toArray(): array
    {
        return array_map(
            static fn (RuleWarning $w): array => $w->toArray(),
            $this->warnings,
        );
    }

    public static function ok(): self
    {
        return new self(warnings: [], bloquant: false);
    }
}
