<?php

declare(strict_types=1);

namespace App\Framework\CircuitBreaker\Events;

use App\Framework\CircuitBreaker\CircuitBreakerMetrics;
use App\Framework\CircuitBreaker\CircuitState;
use App\Framework\Core\ValueObjects\Timestamp;

/**
 * Event fired when a circuit breaker opens (transitions to OPEN state)
 */
final readonly class CircuitBreakerOpened implements CircuitBreakerEvent
{
    public string $eventType;

    public string $severity;

    public CircuitState $newState;

    public Timestamp $occurredAt;

    public function __construct(
        public string $service,
        public string $namespace,
        public CircuitState $previousState,
        public CircuitBreakerMetrics $metrics,
        public ?string $reason = null,
        ?Timestamp $occurredAt = null,
    ) {
        $this->eventType = 'circuit_breaker_opened';
        $this->severity = 'warning';
        $this->newState = CircuitState::OPEN;
        $this->occurredAt = $occurredAt ?? Timestamp::now();
    }

    public function getDescription(): string
    {
        $failures = $this->metrics->failureCount;
        $reason = $this->reason ? " (Reason: {$this->reason})" : '';

        return "Circuit breaker for '{$this->service}' opened after {$failures} failures{$reason}";
    }

    public function toArray(): array
    {
        return [
            'event_type' => $this->eventType,
            'service' => $this->service,
            'namespace' => $this->namespace,
            'previous_state' => $this->previousState->value,
            'new_state' => $this->newState->value,
            'reason' => $this->reason,
            'occurred_at' => $this->occurredAt->toIsoString(),
            'severity' => $this->severity,
            'description' => $this->getDescription(),
            'metrics' => $this->metrics->toArray(),
        ];
    }
}
