<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Dashboard;

use App\Application\LiveComponents\LiveComponentState;

/**
 * Type-safe state for QueueStatsComponent
 *
 * Provides real-time queue statistics and metrics.
 * Immutable value object - transformations return new instances.
 */
final readonly class QueueStatsState implements LiveComponentState
{
    public function __construct(
        public int $currentQueueSize = 0,
        public int $totalJobs = 0,
        public int $successfulJobs = 0,
        public int $failedJobs = 0,
        public float $successRate = 0.0,
        public float $avgExecutionTimeMs = 0.0,
        public string $lastUpdated = ''
    ) {}

    /**
     * Create from array data (from client or storage)
     */
    public static function fromArray(array $data): self
    {
        return new self(
            currentQueueSize: (int) ($data['currentQueueSize'] ?? $data['current_queue_size'] ?? 0),
            totalJobs: (int) ($data['totalJobs'] ?? $data['total_jobs'] ?? 0),
            successfulJobs: (int) ($data['successfulJobs'] ?? $data['successful_jobs'] ?? 0),
            failedJobs: (int) ($data['failedJobs'] ?? $data['failed_jobs'] ?? 0),
            successRate: (float) ($data['successRate'] ?? $data['success_rate'] ?? 0.0),
            avgExecutionTimeMs: (float) ($data['avgExecutionTimeMs'] ?? $data['avg_execution_time_ms'] ?? 0.0),
            lastUpdated: (string) ($data['lastUpdated'] ?? $data['last_updated'] ?? '')
        );
    }

    /**
     * Create empty state with defaults
     */
    public static function empty(): self
    {
        return new self();
    }

    /**
     * Update with fresh queue stats
     */
    public function withStats(
        int $queueSize,
        int $total,
        int $successful,
        int $failed,
        float $avgExecutionTime
    ): self {
        $successRate = $total > 0
            ? round(($successful / $total) * 100, 2)
            : 0.0;

        return new self(
            currentQueueSize: $queueSize,
            totalJobs: $total,
            successfulJobs: $successful,
            failedJobs: $failed,
            successRate: $successRate,
            avgExecutionTimeMs: round($avgExecutionTime, 2),
            lastUpdated: date('H:i:s')
        );
    }

    /**
     * Update timestamp
     */
    public function withLastUpdated(string $timestamp): self
    {
        return new self(
            currentQueueSize: $this->currentQueueSize,
            totalJobs: $this->totalJobs,
            successfulJobs: $this->successfulJobs,
            failedJobs: $this->failedJobs,
            successRate: $this->successRate,
            avgExecutionTimeMs: $this->avgExecutionTimeMs,
            lastUpdated: $timestamp
        );
    }

    /**
     * Convert to array for template rendering
     */
    public function toArray(): array
    {
        return [
            'currentQueueSize' => $this->currentQueueSize,
            'totalJobs' => $this->totalJobs,
            'successfulJobs' => $this->successfulJobs,
            'failedJobs' => $this->failedJobs,
            'successRate' => $this->successRate,
            'avgExecutionTimeMs' => $this->avgExecutionTimeMs,
            'lastUpdated' => $this->lastUpdated,
        ];
    }
}
