export interface Category {
  id: string;
  name: string;
  nameEn: string;
  description?: string;
  descriptionEn?: string;
  sortOrder?: number;
  isActive?: boolean;
  createdAt: string;
  updatedAt: string;
}

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>;
  // Select field
  options?: Array<{ label: string; value: string }>;
  required?: boolean;
  placeholder?: string;
  allowMultiple?: boolean;
  // Text field
  multiline?: boolean;
}

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 TemplateTagLink {
  templateId: string;
  tagId: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  tag: Tag;
}

export type TemplateAssignedTag = Pick<Tag, 'id' | 'name' | 'nameEn' | 'createdAt' | 'updatedAt'>;

export interface Template {
  id: string;
  title: string;
  titleEn: string;
  description: string;
  descriptionEn: string;
  coverImageUrl: string;
  previewUrl: string;
  aspectRatio: string;
  userId: string;
  categoryId: string;
  content?: TemplateGraph | null;
  formSchema?: TemplateFormSchema | null;
  status: string;
  createdAt: string;
  updatedAt: string;
  uploadSapecifications?: string | null;
  uploadSapecificationsEn?: string | null;
  sortOrder?: number;
  costPrice?: number | null;
  price?: number | null;
  category: Category | null;
  tags: Tag[];
  templateTags?: TemplateTagLink[];
}

export interface PaginationMeta {
  page: number;
  size: number;
  total: number;
  totalPages: number;
}

export interface TemplatesResponse {
  success: boolean;
  data: Template[];
  pagination: PaginationMeta;
}

export interface TemplateResponse {
  success: boolean;
  data: Template;
}

export interface CategoryTagLink {
  categoryId: string;
  tagId: string;
  createdAt: string;
  updatedAt: string;
  tag: Tag;
}

export interface CategoryTemplate extends Omit<Template, 'category' | 'tags' | 'templateTags'> {
  templateTags: TemplateTagLink[];
  tags: TemplateAssignedTag[];
}

export interface CategoryWithChildren extends Category {
  description: string;
  descriptionEn: string;
  sortOrder: number;
  isActive: boolean;
  categoryTags: CategoryTagLink[];
  templates: CategoryTemplate[];
  tags: Tag[];
}

export interface CategoriesWithChildrenResponse {
  success: boolean;
  data: CategoryWithChildren[];
}

export interface TemplateGeneration {
  id: string;
  userId: string;
  templateId: string;
  type: 'VIDEO' | 'IMAGE' | 'TEXT';
  resultUrl: string[];
  createdAt: string;
  updatedAt: string;
  status: 'pending' | 'processing' | 'completed' | 'failed';
  creditsCost: number;
  creditsTransactionId: string | null;
  template: {
    id: string;
    title: string;
    titleEn: string;
  };
}

export interface GetTemplateGenerationsParams {
  page?: number;
  limit?: number;
  status?: 'pending' | 'processing' | 'completed' | 'failed';
  templateId?: string;
}

export interface TemplateGenerationsResponseData {
  generations: TemplateGeneration[];
  total: number;
  page: number;
  limit: number;
}

export interface TemplateGenerationsResponse {
  success: boolean;
  data: TemplateGenerationsResponseData;
}

export interface TemplateGenerationResponse {
  success: boolean;
  data: TemplateGeneration;
}
