<?php

declare(strict_types=1);

namespace App\Framework\Database\Profiling;

use App\Framework\Core\ValueObjects\Duration;

/**
 * Detected slow query pattern
 */
final readonly class SlowQueryPattern
{
    public function __construct(
        public SlowQueryPatternType $type,
        public string $description,
        public int $occurrences,
        public Duration $timeWindow
    ) {
    }

    /**
     * Get pattern severity
     */
    public function getSeverity(): string
    {
        return match ($this->type) {
            SlowQueryPatternType::REPEATED_SLOW_QUERY => $this->occurrences > 10 ? 'high' : 'medium',
            SlowQueryPatternType::N_PLUS_ONE => 'high',
            SlowQueryPatternType::MISSING_INDEX => 'medium',
            SlowQueryPatternType::FULL_TABLE_SCAN => 'high'
        };
    }

    /**
     * Convert to array
     */
    public function toArray(): array
    {
        return [
            'type' => $this->type->value,
            'description' => $this->description,
            'occurrences' => $this->occurrences,
            'time_window_seconds' => $this->timeWindow->toSeconds(),
            'severity' => $this->getSeverity(),
        ];
    }
}
