<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Timer;

use App\Application\LiveComponents\LiveComponentState;

/**
 * Type-safe state for TimerComponent
 *
 * Provides compile-time type safety for timer functionality.
 * All properties are readonly and immutable - transformations return new instances.
 */
final readonly class TimerState implements LiveComponentState
{
    public function __construct(
        public int $seconds = 0,
        public bool $isRunning = false,
        public ?int $startedAt = null,
        public array $logs = []
    ) {
    }

    /**
     * Create from ComponentData (Framework → Domain conversion)
     */
    public static function fromArray(array $data): self
    {
        return new self(
            seconds: (int) ($data['seconds'] ?? 0),
            isRunning: (bool) ($data['isRunning'] ?? false),
            startedAt: $data['startedAt'] ?? null,
            logs: ($data['logs'] ?? [])
        );
    }

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

    /**
     * Convert to ComponentData (Domain → Framework conversion)
     */
    public function toArray(): array
    {
        return [
            'seconds' => $this->seconds,
            'isRunning' => $this->isRunning,
            'startedAt' => $this->startedAt,
            'logs' => $this->logs,
        ];
    }

    /**
     * Start the timer
     */
    public function withStarted(): self
    {
        return new self(
            seconds: $this->seconds,
            isRunning: true,
            startedAt: time(),
            logs: $this->addLog('Timer started')
        );
    }

    /**
     * Stop the timer
     */
    public function withStopped(): self
    {
        return new self(
            seconds: $this->seconds,
            isRunning: false,
            startedAt: $this->startedAt,
            logs: $this->addLog('Timer stopped')
        );
    }

    /**
     * Reset the timer
     */
    public function withReset(): self
    {
        return new self(
            seconds: 0,
            isRunning: false,
            startedAt: null,
            logs: $this->addLog('Timer reset')
        );
    }

    /**
     * Tick the timer (increment by one second)
     */
    public function withTick(): self
    {
        if (!$this->isRunning) {
            return $this;
        }

        return new self(
            seconds: $this->seconds + 1,
            isRunning: $this->isRunning,
            startedAt: $this->startedAt,
            logs: $this->logs
        );
    }

    /**
     * Format seconds to MM:SS
     */
    public function getFormattedTime(): string
    {
        $minutes = floor($this->seconds / 60);
        $remainingSeconds = $this->seconds % 60;

        return sprintf('%02d:%02d', $minutes, $remainingSeconds);
    }

    /**
     * Add log entry (immutable)
     */
    private function addLog(string $message): array
    {
        $logs = $this->logs;
        $logs[] = [
            'time' => date('H:i:s'),
            'message' => $message,
        ];

        // Keep only last 5 logs
        return array_slice($logs, -5);
    }
}
