<?php

namespace App\Filament\Resources\DataExportResource\Pages;

use App\Enums\DataExportScope;
use App\Filament\Resources\DataExportResource;
use App\Filament\Resources\Pages\AuditedCreateRecord;
use App\Jobs\GenerateDataExport;
use App\Models\DataExport;
use Filament\Facades\Filament;

class CreateDataExport extends AuditedCreateRecord
{
    protected static string $resource = DataExportResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['user_id'] = Filament::auth()->id();
        $data['status'] = DataExport::STATUS_PENDING;

        if (($data['scope'] ?? null) !== DataExportScope::EVENT->value) {
            $data['event_id'] = null;
        }

        return $data;
    }

    protected function afterCreate(): void
    {
        parent::afterCreate();

        GenerateDataExport::dispatch($this->record->id);
    }
}
