<?php

namespace App\Filament\Resources;

use App\Filament\Resources\StationResource\Pages;
use App\Models\Station;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class StationResource extends Resource
{
    protected static ?string $model = Station::class;

    protected static ?string $navigationIcon = 'heroicon-o-building-office';

    protected static ?string $navigationLabel = '线站管理';

    protected static ?string $modelLabel = '线站';

    protected static ?string $pluralModelLabel = '线站';

    protected static ?int $navigationSort = 1;

    protected static ?string $navigationGroup = '业务管理';

    public static function shouldRegisterNavigation(): bool
    {
        return auth()->user()?->can('station.view') ?? false;
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->label('线站名称')
                    ->required()
                    ->unique(ignoreRecord: true)
                    ->maxLength(255)
                    ->placeholder('例如: BL02U1'),

                Forms\Components\Textarea::make('description')
                    ->label('线站描述')
                    ->rows(3)
                    ->maxLength(65535)
                    ->placeholder('请输入线站描述（可选）')
                    ->columnSpanFull(),

                Forms\Components\Select::make('users')
                    ->label('关联用户')
                    ->relationship('users', 'name')
                    ->multiple()
                    ->searchable()
                    ->preload()
                    ->helperText('关联到此线站的用户只能看到与本线站相关的资源')
                    ->columnSpanFull(),

                Forms\Components\Select::make('guides')
                    ->label('关联指引')
                    ->relationship('guides', 'name')
                    ->multiple()
                    ->searchable()
                    ->preload()
                    ->helperText('选择此线站可用的操作指引')
                    ->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('线站名称')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\TextColumn::make('description')
                    ->label('描述')
                    ->limit(50)
                    ->toggleable(),

                Tables\Columns\TextColumn::make('users_count')
                    ->label('用户数量')
                    ->counts('users')
                    ->sortable(),

                Tables\Columns\TextColumn::make('terminals_count')
                    ->label('终端数量')
                    ->counts('terminals')
                    ->sortable(),

                Tables\Columns\TextColumn::make('knowledge_bases_count')
                    ->label('知识库数量')
                    ->counts('knowledgeBases')
                    ->sortable(),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('创建时间')
                    ->dateTime('Y-m-d H:i:s')
                    ->sortable()
                    ->toggleable(),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->label('编辑'),
                Tables\Actions\DeleteAction::make()
                    ->label('删除'),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->label('批量删除'),
                ]),
            ])
            ->defaultSort('name');
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListStations::route('/'),
            'create' => Pages\CreateStation::route('/create'),
            'edit' => Pages\EditStation::route('/{record}/edit'),
        ];
    }
}
