<?php

namespace App\Filament\Resources\ProductBundleResource\Pages;

use App\Filament\Resources\ProductBundleResource;
use App\Models\Catalog\ProductBundle;
use App\Services\AuditLog;
use Filament\Resources\Pages\EditRecord;

class EditProductBundle extends EditRecord
{
    protected static string $resource = ProductBundleResource::class;

    protected function mutateFormDataBeforeSave(array $data): array
    {
        if (filled($data['branch_id'] ?? null)) {
            ProductBundleResource::assertBranchAccessible((int) $data['branch_id']);
        }

        return $data;
    }

    protected function afterSave(): void
    {
        /** @var ProductBundle $bundle */
        $bundle = $this->record;

        AuditLog::record('product_bundle.updated', $bundle, ['name' => $bundle->name]);
    }
}
