/**
 * 标签 CRUD API
 * P3 标签体系：对接后端标签表
 */
import apiClient from "./client";

export interface TagItem {
  id: string;
  name: string;
  created_at?: string;
  usage_count?: number;
}

/** 获取当前用户所有标签 */
export const getTags = async (): Promise<TagItem[]> => {
  const response = await apiClient.get("/tags");
  return response.data.items || [];
};

/** 创建标签（同名返回 409） */
export const createTag = async (name: string): Promise<TagItem> => {
  const response = await apiClient.post("/tags", { name });
  return response.data;
};

/** 删除标签（同时清理素材关联） */
export const deleteTag = async (tagId: string): Promise<void> => {
  await apiClient.delete(`/tags/${tagId}`);
};

/** 为素材添加标签（最多 50 个） */
export const tagAsset = async (
  assetId: string,
  tagIds: string[],
): Promise<void> => {
  if (tagIds.length === 0) return;
  await apiClient.post(`/assets/${assetId}/tags`, { tag_ids: tagIds });
};

/** 移除素材的某个标签 */
export const untagAsset = async (
  assetId: string,
  tagId: string,
): Promise<void> => {
  await apiClient.delete(`/assets/${assetId}/tags/${tagId}`);
};
