<?php

declare(strict_types=1);

namespace App\Filament\Resources\Experts\Pages;

use App\Filament\Resources\Experts\ExpertResource;
use App\Models\Expert;
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 ViewExpert extends ViewRecord
{
    protected static string $resource = ExpertResource::class;

    protected function getHeaderActions(): array
    {
        return [
            EditAction::make()->label('ویرایش اطلاعات'),
        ];
    }

    public function mount(int|string $record): void
    {
        parent::mount($record);
        $this->getRecord()->load(['user', 'province', 'county', 'educationItems', 'certificates']);
    }

    public function infolist(Schema $schema): Schema
    {
        $record = $this->getRecord();
        $consultationCount = $record->consultationRequests()->count();

        return $schema
            ->components([
                Section::make('اطلاعات شخصی (کارنامه)')
                    ->schema([
                        TextEntry::make('display_name')->label('نام'),
                        TextEntry::make('user.getFilamentName')
                            ->label('نام/نام خانوادگی (کاربر)')
                            ->getStateUsing(fn () => $record->user?->getFilamentName() ?? '-'),
                        TextEntry::make('position')->label('سمت/مقام')->placeholder('-'),
                        TextEntry::make('workplace')->label('محل کار')->placeholder('-'),
                        TextEntry::make('main_category')->label('حوزه فعالیت')->placeholder('-'),
                        TextEntry::make('work_phone')->label('شماره تماس')->placeholder('-'),
                        TextEntry::make('work_email')->label('ایمیل')->placeholder('-'),
                        TextEntry::make('work_address')->label('آدرس')->placeholder('-')->columnSpanFull(),
                        TextEntry::make('status')
                            ->label('وضعیت حساب')
                            ->badge()
                            ->formatStateUsing(fn (?string $state) => Expert::statusOptions()[$state] ?? '-')
                            ->color(fn (?string $state) => match ($state) {
                                Expert::STATUS_APPROVED, Expert::STATUS_ACTIVE => 'success',
                                Expert::STATUS_PENDING => 'warning',
                                Expert::STATUS_BLOCKED => 'danger',
                                default => 'gray',
                            }),
                    ])
                    ->columns(2),

                Section::make('آمار عملکرد')
                    ->schema([
                        TextEntry::make('_consultations')
                            ->label('تعداد مشاوره‌ها')
                            ->getStateUsing(fn () => (string) $consultationCount),
                        TextEntry::make('_satisfaction')
                            ->label('میانگین رضایت')
                            ->getStateUsing(fn () => '—'),
                        TextEntry::make('_projects')
                            ->label('تعداد پروژه‌های انجام‌شده')
                            ->getStateUsing(fn () => '—'),
                    ])
                    ->columns(3),

                Section::make('تحصیلات (مدارک تحصیلی)')
                    ->schema([
                        RepeatableEntry::make('educationItems')
                            ->label('')
                            ->table([
                                TableColumn::make('عنوان'),
                                TableColumn::make('موسسه'),
                                TableColumn::make('سال'),
                            ])
                            ->schema([
                                TextEntry::make('title'),
                                TextEntry::make('institution'),
                                TextEntry::make('year'),
                            ]),
                    ])
                    ->visible(fn () => $record->educationItems->isNotEmpty()),

                Section::make('گواهینامه‌ها / مجوزها')
                    ->schema([
                        RepeatableEntry::make('certificates')
                            ->label('')
                            ->table([
                                TableColumn::make('نام'),
                                TableColumn::make('تایید ادمین'),
                            ])
                            ->schema([
                                TextEntry::make('name'),
                                TextEntry::make('verified')
                                    ->formatStateUsing(fn ($state) => $state ? 'بله' : 'خیر'),
                            ]),
                    ])
                    ->visible(fn () => $record->certificates->isNotEmpty()),
            ]);
    }
}
