<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Branch;
use Brian2694\Toastr\Facades\Toastr;

class BranchInfoController extends Controller
{
    public function index() {
        if (!auth()->user()->can('view_branch_info')) {
            abort(403, 'Unauthorized action.');
        }
        $branches = Branch::with('users')->latest()->get();
        
        return view('admin.branchinfo.index', compact('branches'));
    }

    public function loginAsBranch($branch_id) {
        if (!auth()->user()->hasRole('superadmin')) {
            abort(403, 'Unauthorized action.');
        }

        $user = auth()->user();
        $user->branch_id = $branch_id;
        $user->save();

        Toastr::success('Successfully switched to branch', 'Success');
        return redirect()->route('admin.dashboard');
    }
}
