<?php

declare(strict_types=1);

namespace App\Filament\Resources\HealthTreatments;

use App\Filament\Resources\HealthTreatments\Pages\ListHealthTreatments;
use App\Filament\Resources\HealthTreatments\Pages\ViewHealthTreatment;
use App\Filament\Resources\HealthTreatments\Tables\HealthTreatmentsTable;
use App\Models\HealthTreatment;
use App\Filament\Support\NavigationBadge;
use BackedEnum;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use UnitEnum;

class HealthTreatmentResource extends Resource
{
    protected static ?string $model = HealthTreatment::class;

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

    protected static ?string $navigationLabel = 'محصولات تحت درمان';

    protected static ?string $modelLabel = 'محصول تحت درمان';

    protected static ?string $pluralModelLabel = 'محصولات تحت درمان';

    protected static string|null|UnitEnum $navigationGroup = 'گزارشات و آمار';

    protected static ?int $navigationSort = 12;

    public static function table(Table $table): Table
    {
        return HealthTreatmentsTable::configure($table);
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema->components([
            Section::make('اطلاعات محصول')
                ->schema([
                    TextEntry::make('project.name')->label('پروژه'),
                    TextEntry::make('product_name')->label('نام محصول'),
                    TextEntry::make('product_code')->label('کد محصول'),
                    TextEntry::make('breed_name')->label('نژاد'),
                    TextEntry::make('disease_name')->label('بیماری'),
                    TextEntry::make('status')
                        ->label('وضعیت')
                        ->formatStateUsing(fn (?string $state) => HealthTreatment::statusOptions()[$state] ?? $state),
                    TextEntry::make('progress_percent')->label('پیشرفت (%)'),
                    TextEntry::make('weight')->label('وزن'),
                    TextEntry::make('started_at')->label('شروع')->dateTime('Y/m/d H:i'),
                    TextEntry::make('ended_at')->label('پایان')->dateTime('Y/m/d H:i')->placeholder('-'),
                    TextEntry::make('next_visit_at')->label('مراجعه بعدی')->date('Y/m/d')->placeholder('-'),
                    TextEntry::make('producer.brand_name')->label('خدمات‌دهنده')->placeholder('-'),
                    TextEntry::make('notes')->label('یادداشت')->columnSpanFull()->placeholder('-'),
                ])
                ->columns(3),
            Section::make('تاریخچه پیشرفت')
                ->schema([
                    RepeatableEntry::make('updates')
                        ->label('')
                        ->schema([
                            TextEntry::make('report_at')->label('تاریخ')->dateTime('Y/m/d H:i'),
                            TextEntry::make('progress_percent')->label('پیشرفت (%)'),
                            TextEntry::make('notes')->label('توضیح')->placeholder('-'),
                            TextEntry::make('total_cost')->label('هزینه')->placeholder('-'),
                        ])
                        ->columns(4),
                ]),
        ]);
    }

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

    public static function getNavigationBadge(): ?string
    {
        return NavigationBadge::fromQuery(
            fn () => static::getModel()::query()->whereIn('status', [
                HealthTreatment::STATUS_UNDER_TREATMENT,
                HealthTreatment::STATUS_UNDER_OBSERVATION,
            ])
        );
    }

    public static function getNavigationBadgeColor(): ?string
    {
        return NavigationBadge::colorFromQuery(
            fn () => static::getModel()::query()->whereIn('status', [
                HealthTreatment::STATUS_UNDER_TREATMENT,
                HealthTreatment::STATUS_UNDER_OBSERVATION,
            ]),
            'info'
        );
    }
}
