<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class UserBranchController extends Controller
{
    /**
     * Get all branches that the current user belongs to
     */
    public function getCurrentUserBranches()
    {
        $user = Auth::user();
        $branches = $user->branches;
        
        return response()->json([
            'status' => true,
            'data' => $branches
        ]);
    }
    
    /**
     * Get all branches that the specified user belongs to
     */
    public function getUserBranches(string $id)
    {
        try {
            $user = User::findOrFail($id);
            $branches = $user->branches;
            
            return response()->json([
                'status' => true,
                'data' => $branches
            ]);
        } catch (ModelNotFoundException $e) {
            return response()->json([
                'status' => false,
                'message' => 'User not found'
            ], 404);
        }
    }
}
