<?php

namespace App\Filament\Resources;

use App\Filament\Resources\BusinessTripLocationResource\Pages;
use App\Models\BusinessTripLocation;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class BusinessTripLocationResource extends Resource
{
    protected static ?string $model = BusinessTripLocation::class;

    protected static ?string $navigationIcon = 'heroicon-o-map-pin';

    protected static ?string $navigationGroup = 'Perjalanan Dinas';

    protected static ?string $navigationLabel = 'Lokasi Dinas (Legacy)';

    protected static ?int $navigationSort = 99;

    protected static ?string $recordTitleAttribute = 'name';

    // Sembunyikan dari navigasi - gunakan LocationResource
    protected static bool $shouldRegisterNavigation = false;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Informasi Lokasi')
                    ->schema([
                        Forms\Components\Select::make('company_id')
                            ->label('Perusahaan')
                            ->relationship('company', 'name')
                            ->searchable()
                            ->preload()
                            ->required()
                            ->native(false),
                        Forms\Components\TextInput::make('name')
                            ->label('Nama Lokasi')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Contoh: BPKP Pusat Jakarta'),
                        Forms\Components\TextInput::make('code')
                            ->label('Kode')
                            ->maxLength(50)
                            ->placeholder('Contoh: BPKP-JKT'),
                        Forms\Components\Select::make('type')
                            ->label('Tipe Lokasi')
                            ->options([
                                'bpkp' => 'BPKP',
                                'pemda' => 'Pemerintah Daerah',
                                'kementerian' => 'Kementerian',
                                'lembaga' => 'Lembaga',
                                'other' => 'Lainnya',
                            ])
                            ->default('other')
                            ->native(false)
                            ->required(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Alamat')
                    ->schema([
                        Forms\Components\TextInput::make('city')
                            ->label('Kota')
                            ->maxLength(255)
                            ->placeholder('Jakarta'),
                        Forms\Components\TextInput::make('province')
                            ->label('Provinsi')
                            ->maxLength(255)
                            ->placeholder('DKI Jakarta'),
                        Forms\Components\Textarea::make('address')
                            ->label('Alamat Lengkap')
                            ->rows(2)
                            ->columnSpanFull()
                            ->placeholder('Jl. Pramuka No. 33, Jakarta'),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Koordinat & Radius')
                    ->description('Untuk validasi absensi di lokasi dinas')
                    ->schema([
                        Forms\Components\TextInput::make('latitude')
                            ->label('Latitude')
                            ->numeric()
                            ->step(0.00000001)
                            ->placeholder('-6.175392'),
                        Forms\Components\TextInput::make('longitude')
                            ->label('Longitude')
                            ->numeric()
                            ->step(0.00000001)
                            ->placeholder('106.827153'),
                        Forms\Components\TextInput::make('radius')
                            ->label('Radius (meter)')
                            ->numeric()
                            ->default(500)
                            ->helperText('Radius validasi untuk check-in/check-out'),
                    ])
                    ->columns(3),

                Forms\Components\Section::make('Status & Prioritas')
                    ->schema([
                        Forms\Components\Toggle::make('is_active')
                            ->label('Aktif')
                            ->default(true)
                            ->helperText('Lokasi yang tidak aktif tidak akan muncul di rekomendasi'),
                        Forms\Components\Toggle::make('is_primary')
                            ->label('Lokasi Pilihan Utama')
                            ->default(false)
                            ->helperText('Lokasi ini akan tampil di atas saat user pilih lokasi dinas (contoh: Pusdiklat)'),
                        Forms\Components\TextInput::make('sort_order')
                            ->label('Urutan')
                            ->numeric()
                            ->default(0)
                            ->helperText('Urutan tampil (semakin kecil semakin atas)'),
                    ])
                    ->columns(3),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Nama Lokasi')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('code')
                    ->label('Kode')
                    ->searchable()
                    ->badge()
                    ->color('gray'),
                Tables\Columns\TextColumn::make('type')
                    ->label('Tipe')
                    ->badge()
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'bpkp' => 'BPKP',
                        'pemda' => 'Pemda',
                        'kementerian' => 'Kementerian',
                        'lembaga' => 'Lembaga',
                        default => 'Lainnya',
                    })
                    ->color(fn (string $state): string => match ($state) {
                        'bpkp' => 'primary',
                        'pemda' => 'success',
                        'kementerian' => 'warning',
                        'lembaga' => 'info',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('city')
                    ->label('Kota')
                    ->searchable(),
                Tables\Columns\TextColumn::make('province')
                    ->label('Provinsi')
                    ->searchable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('radius')
                    ->label('Radius')
                    ->suffix(' m')
                    ->sortable(),
                Tables\Columns\IconColumn::make('is_primary')
                    ->label('Utama')
                    ->boolean()
                    ->trueIcon('heroicon-o-star')
                    ->falseIcon('heroicon-o-minus')
                    ->trueColor('warning'),
                Tables\Columns\IconColumn::make('is_active')
                    ->label('Aktif')
                    ->boolean(),
                Tables\Columns\TextColumn::make('company.name')
                    ->label('Perusahaan')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('Dibuat')
                    ->dateTime('d M Y')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('type')
                    ->label('Tipe')
                    ->options([
                        'bpkp' => 'BPKP',
                        'pemda' => 'Pemda',
                        'kementerian' => 'Kementerian',
                        'lembaga' => 'Lembaga',
                        'other' => 'Lainnya',
                    ]),
                Tables\Filters\SelectFilter::make('company_id')
                    ->label('Perusahaan')
                    ->relationship('company', 'name')
                    ->searchable()
                    ->preload(),
                Tables\Filters\TernaryFilter::make('is_active')
                    ->label('Status Aktif'),
                Tables\Filters\TernaryFilter::make('is_primary')
                    ->label('Lokasi Utama'),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('name');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

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

    public static function getGloballySearchableAttributes(): array
    {
        return ['name', 'code', 'city', 'province'];
    }
}
