<?php

declare(strict_types=1);

namespace App\Filament\Resources\HealthReports\Pages;

use App\Filament\Resources\HealthReports\HealthReportResource;
use App\Models\Report;
use Filament\Actions\Action;
use Filament\Resources\Pages\CreateRecord;

class CreateHealthReport extends CreateRecord
{
    protected static string $resource = HealthReportResource::class;

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

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

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['type'] = Report::TYPE_HEALTH;
        if (! empty($data['report_at']) && empty($data['date'])) {
            $data['date'] = \Carbon\Carbon::parse($data['report_at'])->toDateString();
        }
        return $data;
    }

    protected function getCreatedNotificationTitle(): ?string
    {
        if ($this->getRecord() && $this->getRecord()->status === 'draft') {
            return 'گزارش به صورت پیش‌نویس ذخیره شد.';
        }
        return parent::getCreatedNotificationTitle();
    }
}
