<?php

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Resources\Pages\CreateRecord;
use App\Models\User;
use Filament\Notifications\Notification;

class CreateUser extends CreateRecord
{
    protected static string $resource = UserResource::class;
    
    protected function mutateFormDataBeforeCreate(array $data): array
    {
        if (!empty($data['dusun_id']) && User::where('dusun_id', $data['dusun_id'])->exists()) {
            Notification::make()
                ->title('Gagal membuat user')
                ->body('Dusun ini sudah digunakan oleh user lain.')
                ->danger()
                ->send();

            // Hentikan proses create tanpa error HTTP
            $this->halt();
        }

        return $data;
    }
}
