<?php

namespace App\Http\Controllers;

use App\Support\BranchContext;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class BranchSwitchController extends Controller
{
    public function __invoke(Request $request, BranchContext $context): RedirectResponse
    {
        // Only Super Admins may change the active branch; others are pinned.
        abort_unless($request->user()->isSuperAdmin(), 403);

        $request->validate(['branch_id' => ['required']]);

        $context->setBranch($request->input('branch_id'));

        return back()->with('success', 'Active branch updated.');
    }
}
