<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Dashboard;

use App\Application\LiveComponents\LiveComponentState;

/**
 * Type-safe state for SchedulerTimelineComponent
 *
 * Provides real-time scheduled tasks monitoring.
 * Displays upcoming tasks and execution timeline.
 */
final readonly class SchedulerState implements LiveComponentState
{
    public function __construct(
        public int $totalScheduledTasks = 0,
        public int $dueTasks = 0,
        public array $upcomingTasks = [], // [['id' => string, 'next_run' => string, 'schedule_type' => string, 'time_until' => string]]
        public ?string $nextExecution = null,
        public array $statistics = [], // ['total_executions' => int, 'successful' => int, 'failed' => int]
        public string $lastUpdated = ''
    ) {}

    /**
     * Create from array data (from client or storage)
     */
    public static function fromArray(array $data): self
    {
        return new self(
            totalScheduledTasks: (int) ($data['totalScheduledTasks'] ?? $data['total_scheduled_tasks'] ?? 0),
            dueTasks: (int) ($data['dueTasks'] ?? $data['due_tasks'] ?? 0),
            upcomingTasks: (array) ($data['upcomingTasks'] ?? $data['upcoming_tasks'] ?? []),
            nextExecution: $data['nextExecution'] ?? $data['next_execution'] ?? null,
            statistics: (array) ($data['statistics'] ?? []),
            lastUpdated: (string) ($data['lastUpdated'] ?? $data['last_updated'] ?? '')
        );
    }

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

    /**
     * Update with fresh scheduler data
     */
    public function withSchedulerData(
        int $totalScheduledTasks,
        int $dueTasks,
        array $upcomingTasks,
        ?string $nextExecution,
        array $statistics
    ): self {
        return new self(
            totalScheduledTasks: $totalScheduledTasks,
            dueTasks: $dueTasks,
            upcomingTasks: $upcomingTasks,
            nextExecution: $nextExecution,
            statistics: $statistics,
            lastUpdated: date('H:i:s')
        );
    }

    /**
     * Update timestamp
     */
    public function withLastUpdated(string $timestamp): self
    {
        return new self(
            totalScheduledTasks: $this->totalScheduledTasks,
            dueTasks: $this->dueTasks,
            upcomingTasks: $this->upcomingTasks,
            nextExecution: $this->nextExecution,
            statistics: $this->statistics,
            lastUpdated: $timestamp
        );
    }

    /**
     * Convert to array for template rendering
     */
    public function toArray(): array
    {
        return [
            'totalScheduledTasks' => $this->totalScheduledTasks,
            'dueTasks' => $this->dueTasks,
            'upcomingTasks' => $this->upcomingTasks,
            'nextExecution' => $this->nextExecution,
            'statistics' => $this->statistics,
            'lastUpdated' => $this->lastUpdated,
        ];
    }
}
