<?php

namespace App\Filament\Resources\DamageResource\Pages;

use App\Exceptions\StockReferenceException;
use App\Filament\Resources\DamageResource;
use App\Models\Inventory\Damage;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Validation\ValidationException;

class CreateDamage extends CreateRecord
{
    protected static string $resource = DamageResource::class;

    protected function handleRecordCreation(array $data): Damage
    {
        DamageResource::assertBranchAccessible((int) $data['branch_id']);

        try {
            return DamageResource::apply($data);
        } catch (StockReferenceException $e) {
            throw ValidationException::withMessages([$e->field => $e->getMessage()]);
        }
    }
}
