<?php

namespace App\Filament\Resources\SpotRateTransportMasterResource\Pages;

use App\Filament\Resources\SpotRateTransportMasterResource;
use App\Models\SpotRateTransportMaster;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;

class CreateSpotRateTransportMaster extends CreateRecord
{
    protected static string $resource = SpotRateTransportMasterResource::class;

    protected function beforeCreate(): void
    {
        $groupName = $this->data['group_name'] ?? null;
        $userNames = $this->data['user_name'] ?? [];

        foreach ($userNames as $userName) {

            $query = SpotRateTransportMaster::where('group_name', $groupName)
                ->whereJsonContains('user_name', $userName);

            if ($query->exists()) {

                Notification::make()
                    ->title('Duplicate User')
                    ->body("User {$userName} already exists in this group.")
                    ->danger()
                    ->persistent()
                    ->send();

                // Prevent create
                $this->halt();
                return;
            }
        }
    }



}
