<?php

namespace App\Filament\Resources\EventResource\Pages;

use App\Filament\Resources\EventResource;
use App\Filament\Resources\Pages\AuditedCreateRecord;

class CreateEvent extends AuditedCreateRecord
{
    protected static string $resource = EventResource::class;

    public ?int $packageId = null;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $this->packageId = $data['package_id'] ?? null;
        unset($data['package_id']);

        return $data;
    }

    protected function afterCreate(): void
    {
        if ($this->packageId) {
            $this->record->eventPackages()->create([
                'package_id' => $this->packageId,
            ]);
        }

        parent::afterCreate();
    }
}
