import { fetchWithAuth } from "./client";

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

export type ContentStatus =
  | "draft"
  | "review"
  | "approved"
  | "published"
  | "archived";

export type ContentType =
  | "article"
  | "qa"
  | "knowledge_base"
  | "social_post"
  | "other";

export interface Content {
  id: string;
  title: string;
  body: string;
  content_type: ContentType;
  status: ContentStatus;
  project_id?: string;
  author_id: string;
  tags?: string[];
  created_at: string;
  updated_at: string;
  published_at?: string;
}

export interface KnowledgeEntry {
  id: string;
  brand_id: string;
  category: string;
  title: string;
  body: string;
  source?: string;
  created_at: string;
  updated_at: string;
}

export interface CreateContentPayload {
  title: string;
  body: string;
  content_type: ContentType;
  project_id?: string;
  tags?: string[];
}

export interface UpdateContentPayload {
  title?: string;
  body?: string;
  content_type?: ContentType;
  status?: ContentStatus;
  tags?: string[];
}

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

export const contentsApi = {
  list: (token?: string, params?: string) =>
    fetchWithAuth(
      `/api/v1/contents/${params ? `?${params}` : ""}`,
      {},
      token
    ) as Promise<Content[]>,
  get: (token?: string, contentId?: string) =>
    fetchWithAuth(
      `/api/v1/contents/${contentId}`,
      {},
      token
    ) as Promise<Content>,
  create: (token?: string, data?: CreateContentPayload) =>
    fetchWithAuth(
      "/api/v1/contents/",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<Content>,
  update: (token?: string, contentId?: string, data?: UpdateContentPayload) =>
    fetchWithAuth(
      `/api/v1/contents/${contentId}`,
      { method: "PUT", body: JSON.stringify(data) },
      token
    ) as Promise<Content>,
  delete: (token?: string, contentId?: string) =>
    fetchWithAuth(`/api/v1/contents/${contentId}`, { method: "DELETE" }, token),
  publish: (token?: string, contentId?: string) =>
    fetchWithAuth(
      `/api/v1/contents/${contentId}/publish`,
      { method: "POST" },
      token
    ) as Promise<Content>,

  // 品牌知识库
  listKnowledge: (token?: string, brandId?: string) =>
    fetchWithAuth(
      `/api/v1/contents/knowledge/${brandId ? `?brand_id=${brandId}` : ""}`,
      {},
      token
    ) as Promise<KnowledgeEntry[]>,
  createKnowledge: (
    token?: string,
    data?: Omit<KnowledgeEntry, "id" | "created_at" | "updated_at">
  ) =>
    fetchWithAuth(
      "/api/v1/contents/knowledge/",
      { method: "POST", body: JSON.stringify(data) },
      token
    ) as Promise<KnowledgeEntry>,
  updateKnowledge: (
    token?: string,
    knowledgeId?: string,
    data?: Partial<Omit<KnowledgeEntry, "id" | "brand_id" | "created_at" | "updated_at">>
  ) =>
    fetchWithAuth(
      `/api/v1/contents/knowledge/${knowledgeId}`,
      { method: "PUT", body: JSON.stringify(data) },
      token
    ) as Promise<KnowledgeEntry>,
  deleteKnowledge: (token?: string, knowledgeId?: string) =>
    fetchWithAuth(
      `/api/v1/contents/knowledge/${knowledgeId}`,
      { method: "DELETE" },
      token
    ),
};
