<?php

declare(strict_types=1);

namespace App\Filament\Resources\MediaItems\Pages;

use App\Filament\Resources\MediaItems\MediaItemResource;
use App\Models\MediaItem;
use Filament\Actions\EditAction;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Components\ImageEntry;
use Filament\Resources\Pages\ViewRecord;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;

class ViewMediaItem extends ViewRecord
{
    protected static string $resource = MediaItemResource::class;

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

    public function infolist(Schema $schema): Schema
    {
        $record = $this->getRecord();
        return $schema
            ->components([
                Section::make('فایل')
                    ->schema([
                        ImageEntry::make('file_path')
                            ->label('پیش‌نمایش')
                            ->visible(fn () => $record->type === MediaItem::TYPE_IMAGE && $record->file_path),
                        TextEntry::make('file_path')
                            ->label('فایل')
                            ->formatStateUsing(fn ($state) => $state ? 'آپلود شده' : '-')
                            ->visible(fn () => $record->type !== MediaItem::TYPE_IMAGE || ! $record->file_path),
                    ])
                    ->columns(1),
                Section::make('اطلاعات')
                    ->schema([
                        TextEntry::make('name')->label('نام')->placeholder('-'),
                        TextEntry::make('project.name')->label('پروژه')->placeholder('-'),
                        TextEntry::make('type')
                            ->label('نوع فایل')
                            ->formatStateUsing(fn (?string $state) => MediaItem::typeOptions()[$state] ?? '-'),
                        TextEntry::make('category.name')->label('دسته‌بندی')->placeholder('-'),
                        TextEntry::make('subcategory.name')->label('زیردسته')->placeholder('-'),
                        TextEntry::make('uploader_type')
                            ->label('ارسال‌کننده')
                            ->formatStateUsing(fn (?string $state) => MediaItem::uploaderTypeOptions()[$state] ?? '-'),
                        TextEntry::make('status')
                            ->label('وضعیت')
                            ->badge()
                            ->formatStateUsing(fn (?string $state) => MediaItem::statusOptions()[$state] ?? '-'),
                        TextEntry::make('description')->label('توضیحات')->placeholder('-')->columnSpanFull(),
                        TextEntry::make('notes')->label('یادداشت')->placeholder('-')->columnSpanFull(),
                        TextEntry::make('admin_reason')->label('دلیل ادمین')->placeholder('-')->columnSpanFull(),
                    ])
                    ->columns(2),
                Section::make('اطلاعات سند')
                    ->schema([
                        TextEntry::make('start_at')->label('شروع')->dateTime('Y/m/d H:i')->placeholder('-'),
                        TextEntry::make('end_at')->label('خاتمه')->dateTime('Y/m/d H:i')->placeholder('-'),
                        TextEntry::make('contract_number')->label('شماره قرارداد/ثبت')->placeholder('-'),
                        TextEntry::make('company_name')->label('نام شرکت/واسطه')->placeholder('-'),
                        TextEntry::make('contact_phone')->label('شماره تماس')->placeholder('-'),
                    ])
                    ->columns(2)
                    ->visible(fn () => $record->type === MediaItem::TYPE_DOCUMENT),
            ]);
    }
}
