<?php

namespace App\Filament\Resources\Concerns;

use App\Services\CurrentContext;

/**
 * Defence-in-depth server-side branch check for create/edit flows whose
 * branch field is editable in the form. The form select only offers
 * accessible branches, but a crafted submission can still name any branch
 * id — so every page that writes a branch_id the actor chose (as opposed to
 * one pinned by the record itself) asserts it here, in handleRecordCreation
 * / mutateFormDataBeforeSave, before any service call or model write.
 */
trait AssertsBranchAccess
{
    public static function assertBranchAccessible(int $branchId): void
    {
        abort_unless(
            app(CurrentContext::class)->canAccessBranch($branchId),
            403,
            'You do not have access to this branch.',
        );
    }
}