<?php

declare(strict_types=1);

namespace App\Filament\Resources\MediaItems\Pages;

use App\Filament\Resources\MediaItems\MediaItemResource;
use App\Models\MediaItem;
use Filament\Actions\Action;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Auth;

class CreateMediaItem extends CreateRecord
{
    protected static string $resource = MediaItemResource::class;

    protected function getFormActions(): array
    {
        return [
            Action::make('createDraft')
                ->label('ذخیره پیش‌نویس')
                ->color('gray')
                ->action('createDraft'),
            Action::make('create')
                ->label('تایید/ثبت نهایی')
                ->action('submitFinal'),
            $this->getCancelFormAction(),
        ];
    }

    public function createDraft(): void
    {
        $this->form->fill(array_merge($this->form->getState(), ['status' => MediaItem::STATUS_PENDING]));
        $this->create();
    }

    public function submitFinal(): void
    {
        $this->form->fill(array_merge($this->form->getState(), ['status' => MediaItem::STATUS_APPROVED]));
        $this->create();
    }

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['uploader_type'] = MediaItem::UPLOADER_ADMIN;
        $data['uploader_id'] = Auth::id();
        $data['created_by'] = Auth::id();
        $data['status'] = $data['status'] ?? MediaItem::STATUS_PENDING;
        return $data;
    }

    protected function getCreatedNotificationTitle(): ?string
    {
        return 'فایل با موفقیت ثبت شد.';
    }
}
