<?php

declare(strict_types=1);

namespace App\Framework\Filesystem\ValueObjects;

/**
 * Detailed metrics about scanner operations
 */
final readonly class ScannerMetrics
{
    public function __construct(
        public int $totalFilesScanned,
        public int $totalDirectoriesScanned,
        public int $skippedFiles,
        public int $permissionErrors,
        public int $recoverableErrors,
        public int $fatalErrors,
        public int $retryAttempts,
        public ScannerMemoryUsage $memoryStats
    ) {
    }

    public static function empty(): self
    {
        return new self(
            totalFilesScanned: 0,
            totalDirectoriesScanned: 0,
            skippedFiles: 0,
            permissionErrors: 0,
            recoverableErrors: 0,
            fatalErrors: 0,
            retryAttempts: 0,
            memoryStats: ScannerMemoryUsage::current()
        );
    }

    public function getTotalErrors(): int
    {
        return $this->permissionErrors + $this->recoverableErrors + $this->fatalErrors;
    }

    public function getErrorRate(): float
    {
        if ($this->totalFilesScanned === 0) {
            return 0.0;
        }

        return $this->getTotalErrors() / $this->totalFilesScanned;
    }

    public function hasErrors(): bool
    {
        return $this->getTotalErrors() > 0;
    }

    public function hasFatalErrors(): bool
    {
        return $this->fatalErrors > 0;
    }

    public function toArray(): array
    {
        return [
            'total_files_scanned' => $this->totalFilesScanned,
            'total_directories_scanned' => $this->totalDirectoriesScanned,
            'skipped_files' => $this->skippedFiles,
            'permission_errors' => $this->permissionErrors,
            'recoverable_errors' => $this->recoverableErrors,
            'fatal_errors' => $this->fatalErrors,
            'retry_attempts' => $this->retryAttempts,
            'memory_stats' => $this->memoryStats->toArray(),
            'total_errors' => $this->getTotalErrors(),
            'error_rate' => $this->getErrorRate(),
            'has_errors' => $this->hasErrors(),
            'has_fatal_errors' => $this->hasFatalErrors(),
        ];
    }
}
