import type { ReactNode } from "react";

export type WebViewKey =
  | "home"
  | "workbench"
  | "community"
  | "login"
  | "agent"
  | "canvas"
  | "assets"
  | "ecommerceHub"
  | "ecommerce"
  | "ecommerceTemplates"
  | "sizeTemplate"
  | "scriptTokens"
  | "tokenUsage"
  | "imageWorkbench"
  | "resolutionUpscale"
  | "digitalHuman"
  | "avatarConsole"
  | "characterMix"
  | "more"
  | "watermarkRemoval"
  | "subtitleRemoval"
  | "dialogGenerator"
  | "communityReview"
  | "communityCaseAdd"
  | "betaApplications"
  | "report"
  | "providerHealth"
  | "userAgreement"
  | "privacyPolicy"
  | "not-found";

export type WebImageWorkbenchTool = "workbench" | "inpaint" | "camera";

export type WebThemeMode = "light" | "dark";

export type WebAuthMode = "login" | "register";

export interface WebNavItem {
  key: WebViewKey;
  label: string;
  hint: string;
  icon: ReactNode;
  children?: WebNavItem[];
}

export interface WebApiResultMeta {
  source?: "server" | "preview" | "mock-fallback";
  errorMessage?: string;
}

export interface WebUserSession extends WebApiResultMeta {
  token: string;
  user: {
    id: number | string;
    username: string;
    displayName?: string | null;
    bio?: string | null;
    avatarUrl?: string | null;
    backgroundUrl?: string | null;
    email?: string | null;
    emailVerified?: boolean;
    phone?: string | null;
    authProvider?: string | null;
    sessionId?: string | null;
    sessionStartedAt?: string | null;
    role?: string;
    accountType?: "personal" | "enterprise" | string;
    enterpriseId?: string | null;
    enterpriseName?: string | null;
    enterpriseRole?: "admin" | "employee" | string;
    enterpriseAdminUserId?: number | string | null;
    balanceCents?: number;
    enterpriseBalanceCents?: number;
    maxConcurrency?: number;
    activePackages?: Array<{
      name: string;
      expiresAt: string;
      remainingImage: number;
      remainingVideo: number;
      remainingText: number;
    }>;
  };
}

export interface WebProjectSummary extends WebApiResultMeta {
  id: string;
  name: string;
  description?: string | null;
  thumbnailUrl?: string | null;
  updatedAt: string;
  storyboardCount: number;
  imageCount: number;
  videoCount: number;
  mode?: string;
}

export interface WebGenerationPreviewTask extends WebApiResultMeta {
  id: string;
  title: string;
  type: "image" | "video" | "agent" | "digital-human" | "character-mix";
  status: "queued" | "running" | "completed" | "failed";
  progress: number;
  prompt: string;
  createdAt: string;
  projectId?: string;
  outputUrl?: string;
}

export interface WebUsageSummary extends WebApiResultMeta {
  balanceCents: number;
  enterpriseBalanceCents?: number;
  imageUsed: number;
  videoUsed: number;
  textUsed: number;
  betaUnlimited?: boolean;
}

export interface WebEnterpriseUsageMember {
  userId: number | string;
  username: string;
  displayName?: string | null;
  role: "admin" | "employee" | string;
  usedCents: number;
  taskCount: number;
  lastUsedAt?: string | null;
}

export interface WebEnterpriseUsageRecord {
  id: string;
  userId: number | string;
  username: string;
  model: string;
  taskType: "image" | "video" | "text" | string;
  resolution?: string | null;
  durationSeconds?: number | null;
  amountCents: number;
  prompt?: string | null;
  status: string;
  createdAt: string;
}

export interface WebEnterpriseUsageSummary extends WebApiResultMeta {
  enterpriseId: string;
  enterpriseName: string;
  balanceCents: number;
  totalUsedCents: number;
  members: WebEnterpriseUsageMember[];
  modelBreakdown: Array<{ model: string; usedCents: number; taskCount: number }>;
  dailyTrend: Array<{ date: string; usedCents: number; taskCount: number }>;
  records: WebEnterpriseUsageRecord[];
}

export interface ShowcaseItem {
  id: string;
  title: string;
  author: string;
  imageUrl: string;
  tag: string;
}

export interface WebAssetItem {
  id: string;
  type: "character" | "scene" | "prop" | "video" | "image" | "asset" | "other";
  name: string;
  description: string;
  imageUrl: string;
  updatedAt: string;
  status: "ready" | "draft" | "reviewing" | "pending" | "failed";
  url?: string | null;
  ossKey?: string | null;
  tags?: string[];
  sourceTaskId?: string | null;
  sourceProjectId?: string | null;
  metadata?: Record<string, unknown>;
  createdAt?: string;
}

export interface WebCanvasWorkflowNode {
  id: string;
  kind: "prompt" | "text" | "image" | "video" | "model" | "output";
  label: string;
  detail: string;
  position: {
    x: number;
    y: number;
  };
  size?: {
    width: number;
    height: number;
  };
  inputs?: WebCanvasWorkflowPort[];
  outputs?: WebCanvasWorkflowPort[];
  assetRef?: WebCanvasWorkflowAssetRef | null;
  taskRef?: WebCanvasWorkflowTaskRef | null;
  params?: Record<string, unknown>;
  previewUrl?: string;
  metadata?: Record<string, unknown>;
}

export interface WebCanvasWorkflowPort {
  id: string;
  type: "text" | "image" | "video" | "asset";
  label?: string;
}

export interface WebCanvasWorkflowAssetRef {
  url: string;
  ossKey?: string | null;
  mediaType: "text" | "image" | "video" | string;
  sourceTaskId?: string | null;
  originalUrl?: string | null;
  expiresAt?: string | null;
}

export interface WebCanvasWorkflowTaskRef {
  taskId: string;
  status: "queued" | "pending" | "running" | "completed" | "failed" | "cancelled";
  resultUrl?: string | null;
  updatedAt?: string | null;
}

export interface WebCanvasWorkflowEdge {
  id: string;
  source: string;
  target: string;
  sourcePort?: WebCanvasWorkflowPort;
  targetPort?: WebCanvasWorkflowPort;
  label?: string;
  animated?: boolean;
}

export interface WebCanvasWorkflowNodePackage {
  id: string;
  title: string;
  nodeIds: Array<{
    kind: "text" | "image" | "video";
    id: string;
  }>;
  collapsed?: boolean;
  collapsedBounds?: {
    left: number;
    top: number;
    width: number;
    height: number;
  } | null;
  updatedAt?: string;
}

export interface WebCanvasWorkflow {
  id: string;
  version: 1;
  schemaVersion?: 1 | 2;
  title: string;
  description: string;
  author?: string;
  source: "blank" | "community";
  settings: {
    model: string;
    ratio: string;
    duration: string;
    resolution: string;
  };
  viewport?: {
    x: number;
    y: number;
    zoom: number;
  };
  nodes: WebCanvasWorkflowNode[];
  edges: WebCanvasWorkflowEdge[];
  packages?: WebCanvasWorkflowNodePackage[];
}

export interface WebCommunityCase {
  id: string;
  title: string;
  author: string;
  tag: string;
  summary: string;
  imageUrl: string;
  workflow: WebCanvasWorkflow;
  favoriteCount?: number;
  likeCount?: number;
  isFavorited?: boolean;
  isLiked?: boolean;
}

export type WebNotificationType =
  | "task_completed"
  | "task_failed"
  | "review_pending"
  | "review_passed"
  | "review_rejected"
  | "credits_low"
  | "session_expired"
  | "info";

export interface WebNotification {
  id: string;
  type: WebNotificationType;
  title: string;
  description: string;
  createdAt: string;
  isRead: boolean;
  targetView?: WebViewKey;
  targetId?: string;
  targetType?: string | null;
  readAt?: string | null;
  metadata?: Record<string, unknown>;
}
