<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Counter;

use App\Application\LiveComponents\LiveComponentState;

/**
 * Type-safe state for CounterComponent
 *
 * Provides compile-time type safety for component state.
 * All properties are readonly and immutable - transformations return new instances.
 *
 * Implements LiveComponentState interface for framework integration.
 */
final readonly class CounterState implements LiveComponentState
{
    public function __construct(
        public int $count = 0,
        public string $lastUpdate = '',
        public int $renderCount = 0
    ) {
    }

    /**
     * Create from array data (from client or storage)
     */
    public static function fromArray(array $data): self
    {
        return new self(
            count: (int) ($data['count'] ?? 0),
            lastUpdate: (string) ($data['last_update'] ?? ''),
            renderCount: (int) ($data['render_count'] ?? 0)
        );
    }

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

    /**
     * Increment counter (immutable transformation)
     */
    public function increment(): self
    {
        return new self(
            count: $this->count + 1,
            lastUpdate: date('H:i:s'),
            renderCount: $this->renderCount
        );
    }

    /**
     * Decrement counter (immutable transformation)
     */
    public function decrement(): self
    {
        return new self(
            count: $this->count - 1,
            lastUpdate: date('H:i:s'),
            renderCount: $this->renderCount
        );
    }

    /**
     * Add specific amount to counter
     */
    public function addAmount(int $amount): self
    {
        return new self(
            count: $this->count + $amount,
            lastUpdate: date('H:i:s'),
            renderCount: $this->renderCount
        );
    }

    /**
     * Reset counter to zero
     */
    public function reset(): self
    {
        return new self(
            count: 0,
            lastUpdate: date('H:i:s'),
            renderCount: $this->renderCount
        );
    }

    /**
     * Set specific count value
     */
    public function withCount(int $count): self
    {
        return new self(
            count: $count,
            lastUpdate: $this->lastUpdate,
            renderCount: $this->renderCount
        );
    }

    /**
     * Update last update timestamp
     */
    public function withLastUpdate(string $timestamp): self
    {
        return new self(
            count: $this->count,
            lastUpdate: $timestamp,
            renderCount: $this->renderCount
        );
    }

    /**
     * Increment render count (for tracking component renders)
     */
    public function incrementRenderCount(): self
    {
        return new self(
            count: $this->count,
            lastUpdate: $this->lastUpdate,
            renderCount: $this->renderCount + 1
        );
    }

    /**
     * Convert to array for template rendering
     */
    public function toArray(): array
    {
        return [
            'count' => $this->count,
            'last_update' => $this->lastUpdate,
            'render_count' => $this->renderCount,
        ];
    }
}
