/**
 * 注册体系 API 调用（租户/店铺/简写ID/同步）。
 *
 * 复用 apiClient（已含 JWT 拦截器 + 响应解包）。
 * 端点前缀：/admin
 */

import { apiClient } from "./client";

/* ------------------------------------------------------------------ */
/*  类型定义                                                           */
/* ------------------------------------------------------------------ */

/** 租户列表项（后端 CamelModel 序列化为 camelCase） */
export interface TenantItem {
  id: number;
  tenantId: number;
  tenantName: string;
  connectorName: string;
  isActive: boolean;
}

/** 店铺列表项 */
export interface SiteItem {
  id: number;
  siteId: number;
  siteName: string;
  siteCode: string | null;
  siteLabel: string | null;
  isActive: boolean;
}

/** 设置/修改简写ID 请求 */
export interface UpdateSiteCodeRequest {
  newCode: string;
}

/** 简写ID 修改结果 */
export interface SiteCodeResult {
  siteId: number;
  oldCode: string | null;
  newCode: string;
  historyCleaned: boolean;
}

/** 简写ID 变更历史条目 */
export interface SiteCodeHistoryItem {
  id: number;
  siteCode: string;
  isCurrent: boolean;
  createdAt: string;
  retiredAt: string | null;
}

/** 店铺同步结果 */
export interface SiteSyncResult {
  inserted: number;
  updated: number;
}

/* ------------------------------------------------------------------ */
/*  API 调用                                                           */
/* ------------------------------------------------------------------ */

/** 获取所有活跃租户列表 */
export async function fetchTenants(): Promise<TenantItem[]> {
  const { data } = await apiClient.get<TenantItem[]>("/admin/tenants");
  return data;
}

/** 获取指定租户下所有活跃店铺 */
export async function fetchTenantSites(
  tenantId: number,
): Promise<SiteItem[]> {
  const { data } = await apiClient.get<SiteItem[]>(
    `/admin/tenants/${tenantId}/sites`,
  );
  return data;
}

/** 设置/修改店铺简写ID */
export async function updateSiteCode(
  siteId: number,
  newCode: string,
): Promise<SiteCodeResult> {
  const { data } = await apiClient.put<SiteCodeResult>(
    `/admin/sites/${siteId}/site-code`,
    { newCode } satisfies UpdateSiteCodeRequest,
  );
  return data;
}

/** 查看简写ID 变更历史 */
export async function fetchSiteCodeHistory(
  siteId: number,
): Promise<SiteCodeHistoryItem[]> {
  const { data } = await apiClient.get<SiteCodeHistoryItem[]>(
    `/admin/sites/${siteId}/site-code-history`,
  );
  return data;
}

/** 手动触发店铺同步 */
export async function syncSites(): Promise<SiteSyncResult> {
  const { data } = await apiClient.post<SiteSyncResult>(
    "/admin/sites/sync",
  );
  return data;
}

/* ------------------------------------------------------------------ */
/*  测试功能：手动创建/删除店铺                                         */
/* ------------------------------------------------------------------ */

/** 创建店铺请求 */
export interface CreateSitePayload {
  tenantId: number;
  siteId: number;
  siteName: string;
  siteCode?: string;
}

/** 手动创建店铺（测试功能） */
export async function createSite(
  payload: CreateSitePayload,
): Promise<SiteItem> {
  const { data } = await apiClient.post<SiteItem>("/admin/sites", payload);
  return data;
}

/** 删除店铺（测试功能，硬删除） */
export async function deleteSite(id: number): Promise<void> {
  await apiClient.delete(`/admin/sites/${id}`);
}
