import { apiClient } from './client';

// 标签基础信息
export interface Tag {
  id: string;
  name: string;
  nameEn: string;
  description: string;
  descriptionEn: string;
  createdAt: string;
  updatedAt: string;
}

// 模板媒体资源
export interface TemplateMediaAsset {
  url: string;
}

// 模板节点输出
export interface TemplateNodeOutput {
  images?: TemplateMediaAsset[];
  videos?: TemplateMediaAsset[];
  texts?: string[];
  coverUrl?: string;
}

// 模板节点动作数据
export interface TemplateNodeActionData {
  n?: number;
  prompt?: string;
  duration?: string;
  aspectRatio?: string;
  selectedModel?: string;
  advancedParams?: Record<string, string | number | boolean | null>;
}

// 模板节点指标
export interface TemplateNodeMetrics {
  width: number;
  height: number;
}

// 模板节点位置
export interface TemplateNodePosition {
  x: number;
  y: number;
}

// 模板图形节点数据
export interface TemplateGraphNodeData {
  label?: string;
  text?: string;
  description?: string;
  width?: number;
  height?: number;
  flowType?: string;
  autoPlay?: boolean;
  controls?: boolean;
  output?: TemplateNodeOutput;
  actionData?: TemplateNodeActionData;
}

// 模板图形节点
export interface TemplateGraphNode {
  id: string;
  data: TemplateGraphNodeData;
  type: string;
  dragging: boolean;
  measured: TemplateNodeMetrics;
  position: TemplateNodePosition;
  selected: boolean;
}

// 模板图形边
export interface TemplateGraphEdge {
  id: string;
  source: string;
  target: string;
  sourceHandle: string;
  targetHandle: string;
  selected: boolean;
}

// 模板图形视口
export interface TemplateGraphViewport {
  x: number;
  y: number;
  zoom: number;
}

// 模板图形
export interface TemplateGraph {
  edges: TemplateGraphEdge[];
  nodes: TemplateGraphNode[];
  viewport: TemplateGraphViewport;
}

// 模板表单架构
export interface TemplateFormSchema {
  startNodes: TemplateGraphNode[];
  endNodes: TemplateGraphNode[];
}

// 分类信息
export interface Category {
  id: string;
  name: string;
  nameEn: string;
}

// 关联标签（简化版）
export interface TagRef {
  id: string;
  name: string;
  nameEn: string;
}

// 标签关联的模板
export interface TagTemplate {
  id: string;
  title: string;
  titleEn: string;
  sortOrder: number;
  description: string;
  descriptionEn: string;
  coverImageUrl: string;
  previewUrl: string;
  aspectRatio: string;
  content: TemplateGraph;
  formSchema: TemplateFormSchema;
  costPrice: number | null;
  price: number | null;
  createdAt: string;
  updatedAt: string;
  category: Category;
  tags: TagRef[];
}

// 带模板的标签
export interface TagWithTemplates extends Tag {
  templates: TagTemplate[];
}

// 标签响应
export interface TagResponse {
  success: boolean;
  data: TagWithTemplates;
}

// API 函数

export interface GetTagByNameParams {
  name: string;
}

export async function getTagByName(name: string): Promise<TagResponse> {
  return apiClient<TagResponse>(`/api/tags/by-name/${encodeURIComponent(name)}`);
}
