<?php

declare(strict_types=1);

namespace App\Filament\Resources\ProjectCompletions\Tables;

use App\Models\Project;
use App\Models\ProjectCompletion;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class ProjectCompletionsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(fn ($query) => $query->with(['project', 'startedBy', 'finalizedBy']))
            ->defaultSort('started_at', 'desc')
            ->columns([
                TextColumn::make('id')->label('#')->sortable(),
                TextColumn::make('project.code')->label('کد پروژه')->sortable(),
                TextColumn::make('project.name')->label('پروژه')->searchable()->limit(40),
                TextColumn::make('project.status')
                    ->label('وضعیت پروژه')
                    ->badge()
                    ->formatStateUsing(fn (?string $state) => Project::statusOptions()[$state] ?? $state),
                IconColumn::make('producer_confirmed_at')->label('تأیید تولیدکننده')->boolean()
                    ->getStateUsing(fn (ProjectCompletion $r) => $r->producer_confirmed_at !== null),
                IconColumn::make('employer_confirmed_at')->label('تأیید کارفرما')->boolean()
                    ->getStateUsing(fn (ProjectCompletion $r) => $r->employer_confirmed_at !== null),
                IconColumn::make('finalized_at')->label('نهایی شده')->boolean()
                    ->getStateUsing(fn (ProjectCompletion $r) => $r->finalized_at !== null),
                TextColumn::make('started_at')->label('شروع')->jalaliDateTime('Y/m/d H:i')->sortable(),
                TextColumn::make('finalized_at')->label('نهایی')->jalaliDateTime('Y/m/d H:i')->placeholder('-'),
            ])
            ->filters([
                TernaryFilter::make('finalized')
                    ->label('نهایی شده')
                    ->queries(
                        true: fn ($q) => $q->whereNotNull('finalized_at'),
                        false: fn ($q) => $q->whereNull('finalized_at'),
                    ),
            ])
            ->recordActions([
                Action::make('finalize')
                    ->label('نهایی‌سازی')
                    ->icon('heroicon-o-check-badge')
                    ->color('success')
                    ->visible(fn (ProjectCompletion $record) => $record->finalized_at === null)
                    ->requiresConfirmation()
                    ->action(function (ProjectCompletion $record): void {
                        $record->update([
                            'finalized_at' => now(),
                            'finalized_by' => Auth::id(),
                        ]);
                        $record->project?->update(['status' => Project::STATUS_COMPLETED]);
                        Notification::make()->title('پروژه نهایی و تکمیل شد.')->success()->send();
                    }),
            ]);
    }
}
