<?php

namespace App\Filament\Resources;

use App\Filament\Resources\OfficeResource\Pages;
use App\Models\Office;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class OfficeResource extends Resource
{
    protected static ?string $model = Office::class;

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

    protected static ?string $navigationGroup = 'Master Data';

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

    protected static ?int $navigationSort = 99;

    // 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 Kantor')
                    ->schema([
                        Forms\Components\Select::make('company_id')
                            ->label('Perusahaan')
                            ->relationship('company', 'name')
                            ->searchable()
                            ->preload()
                            ->helperText('Kosongkan untuk lokasi umum (BPKP/Pemda)'),
                        Forms\Components\TextInput::make('name')
                            ->label('Nama Kantor/Lokasi')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\TextInput::make('code')
                            ->label('Kode')
                            ->maxLength(255)
                            ->unique(ignoreRecord: true),
                        Forms\Components\Select::make('type')
                            ->label('Tipe Lokasi')
                            ->options([
                                'headquarters' => 'Kantor Pusat',
                                'branch' => 'Kantor Cabang',
                                'bpkp' => 'BPKP Perwakilan',
                                'pemda' => 'Pemerintah Daerah',
                                'other' => 'Lainnya',
                            ])
                            ->required()
                            ->searchable(),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Alamat')
                    ->schema([
                        Forms\Components\Textarea::make('address')
                            ->label('Alamat Lengkap')
                            ->columnSpanFull(),
                        Forms\Components\TextInput::make('city')
                            ->label('Kota/Kabupaten')
                            ->maxLength(255),
                        Forms\Components\TextInput::make('province')
                            ->label('Provinsi')
                            ->required()
                            ->maxLength(255),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Koordinat & Radius')
                    ->schema([
                        Forms\Components\TextInput::make('latitude')
                            ->label('Latitude')
                            ->required()
                            ->numeric()
                            ->step(0.00000001),
                        Forms\Components\TextInput::make('longitude')
                            ->label('Longitude')
                            ->required()
                            ->numeric()
                            ->step(0.00000001),
                        Forms\Components\TextInput::make('radius')
                            ->label('Radius (meter)')
                            ->required()
                            ->numeric()
                            ->default(100)
                            ->helperText('Radius validasi lokasi absensi'),
                    ])
                    ->columns(3),

                Forms\Components\Section::make('Status')
                    ->schema([
                        Forms\Components\Toggle::make('is_active')
                            ->label('Aktif')
                            ->default(true),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Nama')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('code')
                    ->label('Kode')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('type')
                    ->label('Tipe')
                    ->badge()
                    ->formatStateUsing(fn(string $state): string => match ($state) {
                        'headquarters' => 'Kantor Pusat',
                        'branch' => 'Cabang',
                        'bpkp' => 'BPKP',
                        'pemda' => 'Pemda',
                        'other' => 'Lainnya',
                        default => $state,
                    })
                    ->color(fn(string $state): string => match ($state) {
                        'headquarters' => 'primary',
                        'branch' => 'info',
                        'bpkp' => 'success',
                        'pemda' => 'warning',
                        default => 'gray',
                    }),
                Tables\Columns\TextColumn::make('city')
                    ->label('Kota')
                    ->searchable(),
                Tables\Columns\TextColumn::make('province')
                    ->label('Provinsi')
                    ->searchable(),
                Tables\Columns\TextColumn::make('company.name')
                    ->label('Perusahaan')
                    ->placeholder('Umum')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('radius')
                    ->label('Radius')
                    ->suffix(' m')
                    ->sortable(),
                Tables\Columns\IconColumn::make('is_active')
                    ->label('Aktif')
                    ->boolean(),
                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([
                        'headquarters' => 'Kantor Pusat',
                        'branch' => 'Kantor Cabang',
                        'bpkp' => 'BPKP Perwakilan',
                        'pemda' => 'Pemerintah Daerah',
                        'other' => 'Lainnya',
                    ]),
                Tables\Filters\SelectFilter::make('province')
                    ->label('Provinsi')
                    ->options(fn() => Office::distinct()->pluck('province', 'province')->toArray()),
                Tables\Filters\TernaryFilter::make('is_active')
                    ->label('Status Aktif'),
                Tables\Filters\TernaryFilter::make('company_id')
                    ->label('Lokasi Umum')
                    ->placeholder('Semua')
                    ->trueLabel('Lokasi Umum (BPKP/Pemda)')
                    ->falseLabel('Milik Perusahaan')
                    ->queries(
                        true: fn($query) => $query->whereNull('company_id'),
                        false: fn($query) => $query->whereNotNull('company_id'),
                    ),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::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\ListOffices::route('/'),
            'create' => Pages\CreateOffice::route('/create'),
            'edit' => Pages\EditOffice::route('/{record}/edit'),
        ];
    }
}
