<?php

namespace App\Filament\Resources\Finance\Payrolls;

use App\Enums\IsPaid;
use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Finance\Payrolls\Pages\ManagePayrolls;
use App\Models\Payroll;
use BackedEnum;
use Carbon\Carbon;
use Filament\Actions\BulkActionGroup;
use Filament\Resources\Resource;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

class PayrollResource extends Resource
{
    protected static ?string $model = Payroll::class;

    protected static string|UnitEnum|null $navigationGroup = 'Keuangan';

    protected static string|BackedEnum|null $navigationIcon = Heroicon::Banknotes;

    protected static ?string $navigationLabel = 'Penggajian';

    protected static ?int $navigationSort = 2;

    protected static ?string $recordTitleAttribute = 'period_month';

    protected static ?string $slug = 'finance/payrolls';

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.name')
                    ->label('Karyawan')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('period_month')
                    ->label('Periode')
                    ->sortable()
                    ->formatStateUsing(fn ($state) => Carbon::parse($state)->translatedFormat('F Y')),

                TextColumn::make('total_salary')
                    ->label('Total Gaji')
                    ->money('IDR', decimalPlaces: 0)
                    ->sortable(),

                TextColumn::make('is_paid')
                    ->label('Status')
                    ->badge()
                    ->color(fn (IsPaid $state): string => match ($state) {
                        IsPaid::PAID => 'success',
                        IsPaid::NOT_PAID => 'danger',
                        default => 'gray',
                    }),

                TextColumn::make('paid_at')
                    ->label('Tanggal Bayar')
                    ->dateTime('l, d F Y')
                    ->sortable()
                    ->toggleable(),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()
                    ->native(false)
                    ->visible(fn (): bool => auth()->user() && auth()->user()->hasRole(RoleEnum::DEVELOPER)),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::Large),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Penggajian'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::Banknotes)
            ->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false);
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()->paid();
    }

    public static function getPages(): array
    {
        return [
            'index' => ManagePayrolls::route('/'),
        ];
    }

    public static function getRecordRouteBindingEloquentQuery(): Builder
    {
        return parent::getRecordRouteBindingEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
