<?php

declare(strict_types=1);

namespace App\Application\LiveComponents\Tabs;

use App\Framework\LiveComponents\Attributes\Action;
use App\Framework\LiveComponents\Attributes\LiveComponent;
use App\Framework\LiveComponents\Contracts\LiveComponentContract;
use App\Framework\LiveComponents\ValueObjects\ComponentId;
use App\Framework\LiveComponents\ValueObjects\ComponentRenderData;

/**
 * TabsComponent - Dynamic tab management with add/remove functionality
 *
 * Demonstrates:
 * - Type-safe state management with TabsState Value Object
 * - Dynamic tab creation and removal
 * - Tab switching with active state
 * - Content per tab
 * - Closable tabs (optional)
 * - Tab reordering
 * - Maximum tab limit
 * - Compile-time type safety for data access
 */
#[LiveComponent('tabs')]
final readonly class TabsComponent implements LiveComponentContract
{
    /**
     * Create Tabs with explicit, type-safe parameters
     */
    public function __construct(
        public ComponentId $id,
        public TabsState $state
    ) {
    }

    public function getRenderData(): ComponentRenderData
    {
        return new ComponentRenderData(
            templatePath: 'livecomponent-tabs',
            data: [
                // LiveComponent integration
                'componentId' => $this->id->toString(),
                'stateJson' => json_encode($this->state->toArray()),

                // Raw data
                'tabs' => $this->state->tabs,
                'active_tab' => $this->state->activeTab,
                'max_tabs' => $this->state->maxTabs,
                'allow_close' => $this->state->allowClose,
                'allow_add' => $this->state->allowAdd,
                'tab_style' => $this->state->tabStyle,
                'error' => $this->state->error,

                // Display data (pre-computed)
                'prepared_tabs' => $this->state->getPreparedTabs(),
                'has_tabs' => $this->state->hasTabs(),
                'can_add_tab' => $this->state->canAddTab(),
                'show_add_button' => $this->state->canAddTab(),
                'tab_count' => $this->state->getTabCount(),
                'has_error' => $this->state->error !== null,
                'active_tab_data' => $this->state->getActiveTabData(),
            ]
        );
    }

    // Action methods - Type-safe parameters

    #[Action(description: 'Switch to different tab')]
    public function switchTab(string $tabId): TabsState
    {
        return $this->state->withSwitchedTab($tabId);
    }

    #[Action(description: 'Add new tab')]
    public function addTab(string $title, string $content = ''): TabsState
    {
        return $this->state->withTabAdded($title, $content);
    }

    #[Action(description: 'Close tab')]
    public function closeTab(string $tabId): TabsState
    {
        return $this->state->withTabClosed($tabId);
    }

    #[Action(description: 'Update tab content')]
    public function updateTabContent(string $tabId, string $content): TabsState
    {
        return $this->state->withTabContentUpdated($tabId, $content);
    }

    #[Action(description: 'Update tab title')]
    public function updateTabTitle(string $tabId, string $title): TabsState
    {
        return $this->state->withTabTitleUpdated($tabId, $title);
    }

    #[Action(description: 'Reorder tabs')]
    public function reorderTabs(array $tabOrder): TabsState
    {
        return $this->state->withTabsReordered($tabOrder);
    }
}
