<?php

declare(strict_types=1);

namespace App\Framework\LiveComponents\ValueObjects;

/**
 * Scan Result
 *
 * Result von Virus-/Malware-Scans mit Status und optionalen Details.
 * Immutable Value Object für Scanner-Integration.
 */
final readonly class ScanResult
{
    public function __construct(
        public ScanStatus $status,
        public ?string $details = null,
        public ?string $threatName = null,
        public ?float $confidenceScore = null
    ) {
        if ($this->confidenceScore !== null && ($this->confidenceScore < 0.0 || $this->confidenceScore > 1.0)) {
            throw new \InvalidArgumentException('Confidence score must be between 0.0 and 1.0');
        }
    }

    /**
     * Create clean scan result
     */
    public static function clean(?string $details = null): self
    {
        return new self(
            status: ScanStatus::CLEAN,
            details: $details
        );
    }

    /**
     * Create infected scan result
     */
    public static function infected(string $threatName, ?float $confidenceScore = null, ?string $details = null): self
    {
        return new self(
            status: ScanStatus::INFECTED,
            details: $details,
            threatName: $threatName,
            confidenceScore: $confidenceScore
        );
    }

    /**
     * Create suspicious scan result
     */
    public static function suspicious(?string $details = null, ?float $confidenceScore = null): self
    {
        return new self(
            status: ScanStatus::SUSPICIOUS,
            details: $details,
            confidenceScore: $confidenceScore
        );
    }

    /**
     * Create error scan result
     */
    public static function error(string $details): self
    {
        return new self(
            status: ScanStatus::ERROR,
            details: $details
        );
    }

    /**
     * Check if scan was clean
     */
    public function isClean(): bool
    {
        return $this->status === ScanStatus::CLEAN;
    }

    /**
     * Check if file is infected
     */
    public function isInfected(): bool
    {
        return $this->status === ScanStatus::INFECTED;
    }

    /**
     * Check if file is suspicious
     */
    public function isSuspicious(): bool
    {
        return $this->status === ScanStatus::SUSPICIOUS;
    }

    /**
     * Check if scan encountered an error
     */
    public function hasError(): bool
    {
        return $this->status === ScanStatus::ERROR;
    }

    /**
     * Check if file should be quarantined
     */
    public function shouldQuarantine(): bool
    {
        return $this->isInfected() || $this->isSuspicious();
    }

    /**
     * Get recommended quarantine status based on scan result
     */
    public function getRecommendedQuarantineStatus(): QuarantineStatus
    {
        return match ($this->status) {
            ScanStatus::CLEAN => QuarantineStatus::APPROVED,
            ScanStatus::INFECTED, ScanStatus::SUSPICIOUS => QuarantineStatus::REJECTED,
            ScanStatus::ERROR => QuarantineStatus::PENDING, // Re-scan needed
        };
    }

    /**
     * Convert to array for serialization
     */
    public function toArray(): array
    {
        return [
            'status' => $this->status->value,
            'details' => $this->details,
            'threat_name' => $this->threatName,
            'confidence_score' => $this->confidenceScore,
        ];
    }
}
