<?php

declare(strict_types=1);

namespace PpPhp\Core\Diagnostics\Results;

use PpPhp\Core\Diagnostics\Severity;

final class DiagnosticIssue
{
    public function __construct(
        public readonly string $severity,
        public readonly string $message,
        public readonly string $source = '',
        public readonly string $suggestion = '',
        public readonly bool $repairable = false,
        public readonly array $metadata = []
    ) {
    }

    public static function info(string $message, string $source = '', string $suggestion = '', array $metadata = []): self
    {
        return new self(Severity::INFO, $message, $source, $suggestion, false, $metadata);
    }

    public static function warning(string $message, string $source = '', string $suggestion = '', bool $repairable = false, array $metadata = []): self
    {
        return new self(Severity::WARNING, $message, $source, $suggestion, $repairable, $metadata);
    }

    public static function error(string $message, string $source = '', string $suggestion = '', bool $repairable = false, array $metadata = []): self
    {
        return new self(Severity::ERROR, $message, $source, $suggestion, $repairable, $metadata);
    }

    public static function critical(string $message, string $source = '', string $suggestion = '', bool $repairable = false, array $metadata = []): self
    {
        return new self(Severity::CRITICAL, $message, $source, $suggestion, $repairable, $metadata);
    }

    public function isProblem(): bool
    {
        return Severity::isProblem($this->severity);
    }

    public function toArray(): array
    {
        return [
            'severity' => $this->severity,
            'message' => $this->message,
            'source' => $this->source,
            'suggestion' => $this->suggestion,
            'repairable' => $this->repairable,
            'metadata' => $this->metadata,
        ];
    }
}
