<?php

declare(strict_types=1);

namespace App\Filament\Resources\HealthReports\Pages;

use App\Filament\Resources\HealthReports\HealthReportResource;
use App\Models\Report;
use Filament\Actions\EditAction;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\RepeatableEntry\TableColumn;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class ViewHealthReport extends ViewRecord
{
    protected static string $resource = HealthReportResource::class;

    protected function getHeaderActions(): array
    {
        return [
            EditAction::make(),
        ];
    }

    public function mount(int|string $record): void
    {
        parent::mount($record);
        $this->getRecord()->load(['items.product', 'consumables', 'invoiceItems', 'project', 'producer']);
    }

    public function infolist(Schema $schema): Schema
    {
        $record = $this->getRecord();
        return $schema
            ->components([
                Section::make('اطلاعات اصلی')
                    ->schema([
                        TextEntry::make('title')->label('عنوان'),
                        TextEntry::make('project.name')->label('پروژه')->placeholder('-'),
                        TextEntry::make('report_at')->label('تاریخ/زمان')->dateTime('Y/m/d H:i'),
                        TextEntry::make('producer.brand_name')->label('خدمات‌دهنده')->placeholder('-'),
                        TextEntry::make('disease_name')->label('نام بیماری')->placeholder('-'),
                        TextEntry::make('report_type')
                            ->label('نوع گزارش')
                            ->formatStateUsing(fn (?string $state) => Report::healthReportTypes()[$state] ?? $state ?? '-'),
                        TextEntry::make('workflow_status')
                            ->label('وضعیت رسیدگی')
                            ->badge()
                            ->formatStateUsing(fn (?string $state) => match ($state ?? '') {
                                Report::WORKFLOW_APPROVED => 'تایید',
                                Report::WORKFLOW_REJECTED => 'رد',
                                Report::WORKFLOW_NEEDS_EDIT => 'ویرایش',
                                default => 'انتظار',
                            }),
                        TextEntry::make('admin_reason')->label('دلیل ادمین')->placeholder('-')->columnSpanFull(),
                    ])
                    ->columns(2),
                Section::make('محصولات')
                    ->schema([
                        RepeatableEntry::make('items')
                            ->label('')
                            ->table([TableColumn::make('محصول')])
                            ->schema([TextEntry::make('product.name')]),
                    ])
                    ->visible(fn () => $record->items()->exists()),
                Section::make('تشخیص و نظر کارشناسی')
                    ->schema([
                        TextEntry::make('diagnosis')->label('تشخیص')->placeholder('-')->columnSpanFull(),
                        TextEntry::make('expert_opinion')->label('نظر کارشناسی')->placeholder('-')->columnSpanFull(),
                    ])
                    ->columns(1),
                Section::make('اقلام مصرفی')
                    ->schema([
                        RepeatableEntry::make('consumables')
                            ->label('')
                            ->table([
                                TableColumn::make('نام'),
                                TableColumn::make('واحد'),
                                TableColumn::make('مقدار'),
                            ])
                            ->schema([
                                TextEntry::make('name'),
                                TextEntry::make('unit'),
                                TextEntry::make('quantity'),
                            ]),
                    ])
                    ->visible(fn () => $record->consumables()->exists()),
                Section::make('هزینه و فاکتور')
                    ->schema([
                        TextEntry::make('total_cost')->label('مبلغ کل')->placeholder('-'),
                        RepeatableEntry::make('invoiceItems')
                            ->label('آیتم‌های فاکتور')
                            ->table([
                                TableColumn::make('عنوان'),
                                TableColumn::make('مبلغ'),
                            ])
                            ->schema([
                                TextEntry::make('title'),
                                TextEntry::make('amount'),
                            ]),
                        TextEntry::make('invoice_image')
                            ->label('تصویر فاکتور')
                            ->formatStateUsing(fn ($state) => $state ? 'آپلود شده' : '-')
                            ->placeholder('-'),
                    ])
                    ->columns(2)
                    ->visible(fn () => $record->total_cost || $record->invoiceItems()->exists()),
                Section::make('گزارش و پیوست‌ها')
                    ->schema([
                        TextEntry::make('written_report')->label('گزارش مکتوب')->placeholder('-')->columnSpanFull(),
                        TextEntry::make('desc')->label('یادداشت')->placeholder('-')->columnSpanFull(),
                        TextEntry::make('next_visit_at')->label('تاریخ مراجعه بعدی')->date('Y/m/d')->placeholder('-'),
                        TextEntry::make('product_weight')->label('وزن محصول (kg)')->placeholder('-'),
                    ])
                    ->columns(2),
            ]);
    }
}
