<?php

namespace App\Services\Certification;

class ValidationResult
{
    public string $validator;
    public array $issues = [];
    public array $items = [];
    public int $checked = 0;

    public function __construct(string $validator)
    {
        $this->validator = $validator;
    }

    public function addIssue(
        string $severity,
        string $code,
        string $message,
        array $context = []
    ): void {
        $this->issues[] = new ValidationIssue(
            $severity,
            $code,
            $message,
            $context
        );
    }

    public function addItem(array $item): void
    {
        $this->items[] = $item;
    }

    public function criticalCount(): int
    {
        return count(array_filter(
            $this->issues,
            fn($i) => $i->severity === 'critical'
        ));
    }

    public function warningCount(): int
    {
        return count(array_filter(
            $this->issues,
            fn($i) => $i->severity === 'warning'
        ));
    }

    public function score(): int
    {
        $score = 100;
        $score -= ($this->criticalCount() * 10);
        $score -= ($this->warningCount() * 2);

        return max(0, min(100, $score));
    }

    public function status(): string
    {
        if ($this->criticalCount() > 0) {
            return 'critical';
        }

        if ($this->warningCount() > 0) {
            return 'warning';
        }

        return 'healthy';
    }
}
