<?php

declare(strict_types=1);

namespace App\Administration\Application\Service;

use App\Administration\Domain\Model\HomeworkRules\EnforcementMode;

use function array_map;
use function count;

/**
 * Résultat de la validation des règles de devoirs.
 *
 * Selon le mode d'application, les violations sont des warnings (soft)
 * ou des erreurs bloquantes (hard).
 */
final readonly class HomeworkRulesValidationResult
{
    /**
     * @param RuleViolation[] $violations
     */
    public function __construct(
        public array $violations,
        public EnforcementMode $enforcementMode,
    ) {
    }

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

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

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

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

    public static function ok(EnforcementMode $mode): self
    {
        return new self(violations: [], enforcementMode: $mode);
    }
}
