import { fetchWithAuth } from "./client";

// ── 类型定义 ────────────────────────────────────────────────────────────────────

export type ClientStatus = "active" | "inactive" | "trial";

export interface Client {
  id: string;
  name: string;
  company?: string;
  contact_email?: string;
  contact_phone?: string;
  status: ClientStatus;
  manager_id: string;
  created_at: string;
  updated_at: string;
}

export interface CreateClientPayload {
  name: string;
  company?: string;
  contact_email?: string;
  contact_phone?: string;
}

export interface UpdateClientPayload {
  name?: string;
  company?: string;
  contact_email?: string;
  contact_phone?: string;
  status?: ClientStatus;
}

// ── API ────────────────────────────────────────────────────────────────────────

export const clientsApi = {
  list: (token: string, params?: string) =>
    fetchWithAuth(
      `/api/v1/clients/${params ? `?${params}` : ""}`,
      {},
      token
    ) as Promise<Client[]>,
  get: (token: string, clientId: string) =>
    fetchWithAuth(
      `/api/v1/clients/${clientId}`,
      {},
      token
    ) as Promise<Client>,
  create: (token: string, data: CreateClientPayload) =>
    fetchWithAuth(
      "/api/v1/clients/",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<Client>,
  update: (token: string, clientId: string, data: UpdateClientPayload) =>
    fetchWithAuth(
      `/api/v1/clients/${clientId}`,
      { method: "PUT", body: JSON.stringify(data) },
      token
    ) as Promise<Client>,
  delete: (token: string, clientId: string) =>
    fetchWithAuth(`/api/v1/clients/${clientId}`, { method: "DELETE" }, token),
  getProjects: (token: string, clientId: string) =>
    fetchWithAuth(`/api/v1/clients/${clientId}/projects`, {}, token),
};
