<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ShiftResource\Pages;
use App\Models\Shift;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class ShiftResource extends Resource
{
    protected static ?string $model = Shift::class;

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

    protected static ?string $navigationGroup = 'Pengaturan';

    protected static ?string $navigationLabel = 'Shift Kerja';

    protected static ?string $modelLabel = 'Shift';

    protected static ?string $pluralModelLabel = 'Shift Kerja';

    protected static ?int $navigationSort = 2;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Informasi Dasar')
                    ->description('Lengkapi informasi shift kerja untuk perusahaan')
                    ->icon('heroicon-o-clock')
                    ->schema([
                        Forms\Components\Select::make('company_id')
                            ->label('Perusahaan')
                            ->relationship('company', 'name')
                            ->required()
                            ->searchable()
                            ->preload()
                            ->helperText('Pilih perusahaan yang akan menggunakan shift ini')
                            ->columnSpan(2),
                        Forms\Components\TextInput::make('name')
                            ->label('Nama Shift')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Contoh: Shift Normal, Shift Pagi, Shift Siang')
                            ->helperText('Nama yang mudah diingat untuk shift ini')
                            ->columnSpan(1),
                        Forms\Components\TextInput::make('code')
                            ->label('Kode Shift')
                            ->maxLength(50)
                            ->placeholder('Contoh: SN, SP, SS')
                            ->helperText('Kode singkat untuk identifikasi shift')
                            ->columnSpan(1),
                    ])
                    ->columns(2),
                
                Forms\Components\Section::make('Jam Kerja')
                    ->description('Tentukan waktu masuk dan pulang untuk shift ini')
                    ->icon('heroicon-o-calendar-days')
                    ->schema([
                        Forms\Components\TimePicker::make('start_time')
                            ->label('Jam Masuk')
                            ->required()
                            ->seconds(false)
                            ->helperText('Waktu mulai kerja (format: HH:mm)')
                            ->default('08:00')
                            ->columnSpan(1),
                        Forms\Components\TimePicker::make('end_time')
                            ->label('Jam Pulang')
                            ->required()
                            ->seconds(false)
                            ->helperText('Waktu selesai kerja (format: HH:mm)')
                            ->default('16:00')
                            ->columnSpan(1),
                    ])
                    ->columns(2),
                
                Forms\Components\Section::make('Pengaturan')
                    ->description('Konfigurasi tambahan untuk shift ini')
                    ->icon('heroicon-o-cog-6-tooth')
                    ->schema([
                        Forms\Components\Toggle::make('is_default')
                            ->label('Shift Default')
                            ->helperText('Jika aktif, shift ini akan menjadi default untuk karyawan baru')
                            ->default(false)
                            ->columnSpan(1),
                        Forms\Components\Toggle::make('is_active')
                            ->label('Status Aktif')
                            ->helperText('Nonaktifkan jika shift ini tidak digunakan lagi')
                            ->default(true)
                            ->columnSpan(1),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('company.name')
                    ->label('Perusahaan')
                    ->sortable()
                    ->searchable()
                    ->weight('medium')
                    ->icon('heroicon-o-building-office-2'),
                Tables\Columns\TextColumn::make('name')
                    ->label('Nama Shift')
                    ->searchable()
                    ->sortable()
                    ->weight('semibold')
                    ->icon('heroicon-o-clock'),
                Tables\Columns\TextColumn::make('code')
                    ->label('Kode')
                    ->searchable()
                    ->badge()
                    ->color('gray'),
                Tables\Columns\TextColumn::make('start_time')
                    ->label('Jam Masuk')
                    ->time('H:i')
                    ->icon('heroicon-o-arrow-right-circle')
                    ->color('success'),
                Tables\Columns\TextColumn::make('end_time')
                    ->label('Jam Pulang')
                    ->time('H:i')
                    ->icon('heroicon-o-arrow-left-circle')
                    ->color('danger'),
                Tables\Columns\TextColumn::make('duration')
                    ->label('Durasi')
                    ->getStateUsing(function (Shift $record): string {
                        if (!$record->start_time || !$record->end_time) {
                            return '-';
                        }
                        $minutes = $record->start_time->diffInMinutes($record->end_time);
                        $hours = floor($minutes / 60);
                        $mins = $minutes % 60;
                        return "{$hours}j {$mins}m";
                    })
                    ->badge()
                    ->color('info'),
                Tables\Columns\IconColumn::make('is_default')
                    ->label('Default')
                    ->boolean()
                    ->trueIcon('heroicon-o-star')
                    ->falseIcon('heroicon-o-star')
                    ->trueColor('warning')
                    ->falseColor('gray'),
                Tables\Columns\IconColumn::make('is_active')
                    ->label('Status')
                    ->boolean()
                    ->trueIcon('heroicon-o-check-circle')
                    ->falseIcon('heroicon-o-x-circle')
                    ->trueColor('success')
                    ->falseColor('danger'),
                Tables\Columns\TextColumn::make('users_count')
                    ->label('Karyawan')
                    ->counts('users')
                    ->sortable()
                    ->badge()
                    ->color('primary')
                    ->icon('heroicon-o-users'),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('company_id')
                    ->label('Perusahaan')
                    ->relationship('company', 'name')
                    ->preload()
                    ->searchable(),
                Tables\Filters\TernaryFilter::make('is_default')
                    ->label('Shift Default')
                    ->placeholder('Semua')
                    ->trueLabel('Default saja')
                    ->falseLabel('Bukan default'),
                Tables\Filters\TernaryFilter::make('is_active')
                    ->label('Status Aktif')
                    ->placeholder('Semua')
                    ->trueLabel('Aktif saja')
                    ->falseLabel('Tidak aktif'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('company.name')
            ->emptyStateHeading('Belum ada shift')
            ->emptyStateDescription('Mulai dengan membuat shift kerja pertama')
            ->emptyStateIcon('heroicon-o-clock');
    }

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

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