<?php

declare(strict_types=1);

namespace App\Filament\Resources\HealthTreatments\Tables;

use App\Filament\Support\SelectOptionLabels;

use App\Models\HealthTreatment;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class HealthTreatmentsTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('project.name')->label('پروژه')->searchable()->sortable(),
                TextColumn::make('product_name')->label('محصول')->searchable(),
                TextColumn::make('product_code')->label('کد')->searchable(),
                TextColumn::make('breed_name')->label('نژاد')->toggleable(),
                TextColumn::make('disease_name')->label('بیماری')->searchable(),
                TextColumn::make('status')
                    ->label('وضعیت')
                    ->badge()
                    ->formatStateUsing(fn (?string $state) => HealthTreatment::statusOptions()[$state] ?? $state)
                    ->color(fn (?string $state) => match ($state) {
                        HealthTreatment::STATUS_UNDER_TREATMENT => 'warning',
                        HealthTreatment::STATUS_UNDER_OBSERVATION => 'info',
                        HealthTreatment::STATUS_COMPLETED => 'success',
                        HealthTreatment::STATUS_REMOVED => 'danger',
                        default => 'gray',
                    }),
                TextColumn::make('progress_percent')->label('پیشرفت %')->sortable(),
                TextColumn::make('producer.brand_name')->label('خدمات‌دهنده')->placeholder('-'),
                TextColumn::make('started_at')->label('شروع')->jalaliDateTime('Y/m/d')->sortable(),
            ])
            ->defaultSort('started_at', 'desc')
            ->filters([
                TrashedFilter::make(),
                SelectFilter::make('project_id')
                    ->label('پروژه')
                    ->relationship('project', 'name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::project($record))
                    ->searchable()
                    ->preload(),
                SelectFilter::make('status')
                    ->label('وضعیت')
                    ->options(HealthTreatment::statusOptions()),
                SelectFilter::make('producer_id')
                    ->label('خدمات‌دهنده')
                    ->relationship('producer', 'brand_name')
                    ->getOptionLabelFromRecordUsing(fn ($record) => SelectOptionLabels::producer($record))
                    ->searchable()
                    ->preload(),
                Filter::make('date_range')
                    ->label('بازه زمانی')
                    ->schema([
                        DatePicker::make('from')->label('از')->jalali(),
                        DatePicker::make('to')->label('تا')->jalali(),
                    ])
                    ->query(function (Builder $q, array $data): Builder {
                        if (filled($data['from'] ?? null)) {
                            $q->whereDate('started_at', '>=', $data['from']);
                        }
                        if (filled($data['to'] ?? null)) {
                            $q->whereDate('started_at', '<=', $data['to']);
                        }

                        return $q;
                    }),
            ])
            ->recordActions([
                ViewAction::make(),
            ]);
    }
}
