<?php

namespace App\Filament\Pages\Integrations;

use BackedEnum;
use Filament\Pages\Page;
use UnitEnum;
use Livewire\Attributes\Computed;
use Illuminate\Http\Request;
use App\Livewire\Integrations\KnowledgeGraphComponent;
use App\Livewire\Integrations\KnowledgePointsListComponent;
use App\Livewire\Integrations\KnowledgePointStatsComponent;
use App\Livewire\Integrations\KnowledgeNetworkComponent;

class KnowledgeGraphIntegration extends Page
{
    protected static ?string $title = '知识图谱整合视图';
    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-globe-alt';
    protected static ?string $navigationLabel = '知识图谱整合';
    protected static string|UnitEnum|null $navigationGroup = null;
    protected static ?int $navigationSort = 7;
    protected static bool $shouldRegisterNavigation = false;
    protected string $view = 'filament.pages.integrations.knowledge-graph-integration';

    public ?string $selectedKpCode = null;
    public string $activeTab = 'graph';
    public bool $showSidebar = true;

    public function mount(Request $request): void
    {
        $this->selectedKpCode = $request->query('kp_code');
        $this->activeTab = $request->query('tab', 'graph');
    }

    #[Computed]
    public function tabs()
    {
        return [
            'graph' => [
                'label' => '知识图谱',
                'icon' => 'heroicon-o-share',
                'component' => 'knowledge-graph',
            ],
            'list' => [
                'label' => '知识点列表',
                'icon' => 'heroicon-o-list-bullet',
                'component' => 'knowledge-points-list',
            ],
            'stats' => [
                'label' => '题库统计',
                'icon' => 'heroicon-o-chart-bar',
                'component' => 'knowledge-point-stats',
            ],
            'network' => [
                'label' => '关联网络',
                'icon' => 'heroicon-o-squares-2x2',
                'component' => 'knowledge-network',
            ],
        ];
    }

    public function setActiveTab(string $tab): void
    {
        $this->activeTab = $tab;
        $this->dispatch('tabChanged', tab: $tab);
    }

    public function toggleSidebar()
    {
        $this->showSidebar = !$this->showSidebar;
    }

    public function clearSelection()
    {
        $this->selectedKpCode = null;
        $this->dispatch('clearAllSelections');
    }

    public function updatedSelectedKpCode($value)
    {
        if ($value) {
            $this->dispatch('kpSelected', kpCode: $value);
        }
    }

    public function getBreadcrumbs(): array
    {
        return [
            'knowledge-graph-integration' => '知识图谱整合视图',
        ];
    }
}
