<?php

declare(strict_types=1);

namespace App\Filament\Resources\ProjectRequests\Pages;

use App\Filament\Resources\ProjectRequests\ProjectRequestResource;
use App\Models\ProjectRequest;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class ViewProjectRequest extends ViewRecord
{
    protected static string $resource = ProjectRequestResource::class;

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

    public function infolist(Schema $schema): Schema
    {
        $record = $this->getRecord();
        $payload = is_array($record->payload) ? $record->payload : [];

        return $schema->components([
            Section::make('اطلاعات درخواست')
                ->schema([
                    TextEntry::make('title')->label('عنوان'),
                    TextEntry::make('producer.brand_name')->label('تولیدکننده')->placeholder('-'),
                    TextEntry::make('status')
                        ->label('وضعیت')
                        ->badge()
                        ->formatStateUsing(fn (?string $state) => ProjectRequest::statusOptions()[$state] ?? $state),
                    TextEntry::make('admin_note')->label('یادداشت ادمین')->placeholder('-')->columnSpanFull(),
                    TextEntry::make('project.name')->label('پروژه مرتبط')->placeholder('-'),
                    TextEntry::make('created_at')->label('تاریخ ثبت')->jalaliDateTime('Y/m/d H:i'),
                    TextEntry::make('reviewed_at')->label('تاریخ بررسی')->jalaliDateTime('Y/m/d H:i')->placeholder('-'),
                ])->columns(2),
            Section::make('جزئیات payload')
                ->schema([
                    TextEntry::make('payload_json')
                        ->label('محتوا')
                        ->getStateUsing(fn () => $payload === [] ? '-' : json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT))
                        ->columnSpanFull(),
                ]),
        ]);
    }
}
