import { apiGet, apiPost, apiPatch, apiDelete } from "@/lib/api/client";
import type { AuthTokenResponse } from "@/lib/api/types";

export interface BranchRoleAssignment {
  id: string;
  branchId: string;
  branchName: string;
  role: string;
}

export function listBranchRoles(cafeId: string, employeeId: string) {
  return apiGet<BranchRoleAssignment[]>(
    `/api/cafes/${cafeId}/employees/${employeeId}/branch-roles`
  );
}

export function assignBranchRole(
  cafeId: string,
  employeeId: string,
  body: { branchId: string; role: string }
) {
  return apiPost<BranchRoleAssignment, typeof body>(
    `/api/cafes/${cafeId}/employees/${employeeId}/branch-roles`,
    body
  );
}

export function updateBranchRole(
  cafeId: string,
  employeeId: string,
  assignmentId: string,
  role: string
) {
  return apiPatch<BranchRoleAssignment, { role: string }>(
    `/api/cafes/${cafeId}/employees/${employeeId}/branch-roles/${assignmentId}`,
    { role }
  );
}

export function removeBranchRole(
  cafeId: string,
  employeeId: string,
  assignmentId: string
) {
  return apiDelete(
    `/api/cafes/${cafeId}/employees/${employeeId}/branch-roles/${assignmentId}`
  );
}

/** Re-issue the session token scoped to a branch (null = café-wide, Owner only). */
export function switchBranch(branchId: string | null) {
  return apiPost<AuthTokenResponse, { branchId: string | null }>(
    `/api/auth/switch-branch`,
    { branchId }
  );
}
