<?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 closes (transitions to CLOSED state)
 */
final readonly class CircuitBreakerClosed 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_closed';
        $this->severity = 'info';
        $this->newState = CircuitState::CLOSED;
        $this->occurredAt = $occurredAt ?? Timestamp::now();
    }

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

        return "Circuit breaker for '{$this->service}' closed after {$successes} successful attempts{$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(),
        ];
    }
}
