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

export interface TableSectionDto {
  id: string;
  branchId: string;
  name: string;
  sortOrder: number;
  isActive: boolean;
  tableCount: number;
}

export function branchTablesPath(cafeId: string, branchId: string): string {
  return `/api/cafes/${cafeId}/branches/${branchId}/tables`;
}

export async function fetchBranchTableBoard(
  cafeId: string,
  branchId: string,
  activeOnly = false
): Promise<TableBoardItem[]> {
  const params = new URLSearchParams({ activeOnly: String(activeOnly) });
  return apiGet<TableBoardItem[]>(
    `${branchTablesPath(cafeId, branchId)}/board?${params}`
  );
}

/** POS + admin board: café-wide endpoint (optional branch filter), with fallback if branch has no rows. */
export async function fetchCafeTableBoard(
  cafeId: string,
  branchId?: string | null
): Promise<TableBoardItem[]> {
  const params = new URLSearchParams({ activeOnly: "false" });
  if (branchId) params.set("branchId", branchId);
  const scoped = await apiGet<TableBoardItem[]>(
    `/api/cafes/${cafeId}/tables/board?${params}`
  );
  if (scoped.length > 0 || !branchId) return scoped;
  return apiGet<TableBoardItem[]>(
    `/api/cafes/${cafeId}/tables/board?activeOnly=false`
  );
}

export async function fetchBranchSections(
  cafeId: string,
  branchId: string
): Promise<TableSectionDto[]> {
  return apiGet<TableSectionDto[]>(
    `${branchTablesPath(cafeId, branchId)}/sections`
  );
}

export async function createBranchTable(
  cafeId: string,
  branchId: string,
  body: {
    number: string;
    capacity: number;
    floor?: string | null;
    sectionId?: string | null;
    sortOrder?: number;
  }
): Promise<void> {
  await apiPost(`${branchTablesPath(cafeId, branchId)}`, body);
}

export async function patchBranchTable(
  cafeId: string,
  branchId: string,
  tableId: string,
  body: Record<string, unknown>
): Promise<void> {
  await apiPatch(`${branchTablesPath(cafeId, branchId)}/${tableId}`, body);
}

export async function deleteBranchTable(
  cafeId: string,
  branchId: string,
  tableId: string
): Promise<void> {
  await apiDelete(`${branchTablesPath(cafeId, branchId)}/${tableId}`);
}

export async function setTableCleaning(
  cafeId: string,
  tableId: string,
  isCleaning: boolean,
  branchId?: string | null
): Promise<TableBoardItem> {
  const body = { isCleaning };
  if (branchId) {
    return apiPatch<TableBoardItem>(
      `${branchTablesPath(cafeId, branchId)}/${tableId}/cleaning`,
      body
    );
  }
  return apiPatch<TableBoardItem>(
    `/api/cafes/${cafeId}/tables/${tableId}/cleaning`,
    body
  );
}

export async function createBranchSection(
  cafeId: string,
  branchId: string,
  body: { name: string; sortOrder?: number }
): Promise<TableSectionDto> {
  return apiPost<TableSectionDto>(
    `${branchTablesPath(cafeId, branchId)}/sections`,
    body
  );
}

export async function patchBranchSection(
  cafeId: string,
  branchId: string,
  sectionId: string,
  body: { name?: string; sortOrder?: number }
): Promise<TableSectionDto> {
  return apiPatch<TableSectionDto>(
    `${branchTablesPath(cafeId, branchId)}/sections/${sectionId}`,
    body
  );
}

export async function deleteBranchSection(
  cafeId: string,
  branchId: string,
  sectionId: string
): Promise<void> {
  await apiDelete(
    `${branchTablesPath(cafeId, branchId)}/sections/${sectionId}`
  );
}
