<?php

namespace App\Filament\Resources\UserResource\RelationManagers;

use App\Models\Shift;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class UserShiftsRelationManager extends RelationManager
{
    protected static string $relationship = 'userShifts';

    protected static ?string $title = 'Penugasan Shift';

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

    public function form(Form $form): Form
    {
        $owner = $this->getOwnerRecord();

        return $form
            ->schema([
                Forms\Components\Select::make('shift_id')
                    ->label('Shift')
                    ->options(
                        Shift::where('company_id', $owner->company_id)
                            ->where('is_active', true)
                            ->pluck('name', 'id')
                    )
                    ->required()
                    ->native(false)
                    ->prefixIcon('heroicon-o-clock'),
                Forms\Components\DatePicker::make('effective_date')
                    ->label('Tanggal Mulai')
                    ->required()
                    ->default(now())
                    ->native(false)
                    ->prefixIcon('heroicon-o-calendar'),
                Forms\Components\DatePicker::make('end_date')
                    ->label('Tanggal Selesai')
                    ->nullable()
                    ->native(false)
                    ->prefixIcon('heroicon-o-calendar')
                    ->helperText('Kosongkan jika berlaku seterusnya'),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('shift.name')
            ->columns([
                Tables\Columns\TextColumn::make('shift.name')
                    ->label('Shift')
                    ->weight('bold')
                    ->icon('heroicon-o-clock'),
                Tables\Columns\TextColumn::make('shift.start_time')
                    ->label('Jam Masuk')
                    ->dateTime('H:i'),
                Tables\Columns\TextColumn::make('shift.end_time')
                    ->label('Jam Keluar')
                    ->dateTime('H:i'),
                Tables\Columns\TextColumn::make('effective_date')
                    ->label('Mulai')
                    ->date('d M Y')
                    ->sortable(),
                Tables\Columns\TextColumn::make('end_date')
                    ->label('Selesai')
                    ->date('d M Y')
                    ->placeholder('Seterusnya')
                    ->sortable(),
                Tables\Columns\IconColumn::make('is_active')
                    ->label('Aktif')
                    ->boolean()
                    ->getStateUsing(fn ($record) => $record->isActiveForDate(now()))
                    ->trueIcon('heroicon-o-check-circle')
                    ->falseIcon('heroicon-o-x-circle')
                    ->trueColor('success')
                    ->falseColor('gray'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->label('Assign Shift')
                    ->icon('heroicon-o-plus'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('effective_date', 'desc');
    }
}
