import { HAPPY_HORSE_UI_MODEL } from "./happyHorseRouting";
import { VIDU_UI_MODEL } from "./viduRouting";
import { PIXVERSE_UI_MODEL } from "./pixverseRouting";

export const ENTERPRISE_KLING_MODEL = "kling-3.0-dashscope";
export const ENTERPRISE_WANXIANG_I2V_MODEL = "wan2.7-i2v";

export const ENTERPRISE_VIDEO_MODEL_OPTIONS = [
  {
    value: HAPPY_HORSE_UI_MODEL,
    label: "HappyHorse 1.0",
    description: "自动匹配文生视频、首帧图生视频或参考图生视频",
  },
  {
    value: VIDU_UI_MODEL,
    label: "Vidu Q3 Turbo",
    description: "自动匹配文生视频或图生视频，支持16秒",
  },
  {
    value: PIXVERSE_UI_MODEL,
    label: "PixVerse V6",
    description: "自动匹配文生视频或图生视频，擅长动作特效",
  },
  {
    value: ENTERPRISE_WANXIANG_I2V_MODEL,
    label: "万相 图生视频",
    description: "图生视频模型，支持首帧图驱动",
  },
  {
    value: ENTERPRISE_KLING_MODEL,
    label: "Kling V3 Omni",
    description: "支持文生视频、图生视频及多模态参考生成",
  },
] as const;

export const ENTERPRISE_VIDEO_RESOLUTION_OPTIONS = [
  { value: "720P", label: "720P" },
  { value: "1080P", label: "1080P" },
] as const;

export const ENTERPRISE_DEFAULT_VIDEO_MODEL = HAPPY_HORSE_UI_MODEL;
export const ENTERPRISE_DEFAULT_VIDEO_RESOLUTION = "1080P";

export interface EnterpriseVideoPricingInput {
  model: string;
  resolution: string;
  durationSeconds: number;
  muted?: boolean;
  hasReferenceVideo?: boolean;
}

export function normalizeEnterpriseResolution(value: string): "720P" | "1080P" {
  return String(value || "").toUpperCase() === "720P" ? "720P" : "1080P";
}

export function getEnterpriseVideoCreditRate(input: EnterpriseVideoPricingInput): number {
  const resolution = normalizeEnterpriseResolution(input.resolution);
  const model = String(input.model || "").toLowerCase();

  if (model.includes("happyhorse")) {
    return resolution === "720P" ? 0.72 : 1.28;
  }

  if (model.includes("wan2.7-i2v") || model.includes("wanxiang")) {
    return resolution === "720P" ? 0.6 : 1;
  }

  if (model.includes("animate-mix")) {
    return resolution === "720P" ? 0.6 : 1;
  }

  if (model.includes("s2v")) {
    return resolution === "720P" ? 0.6 : 1;
  }

  if (model.includes("vidu")) {
    return resolution === "720P" ? 0.4 : 0.8;
  }

  if (model.includes("pixverse")) {
    return resolution === "720P" ? 0.4 : 0.8;
  }

  if (model.includes("kling")) {
    if (input.muted) {
      if (input.hasReferenceVideo) return resolution === "720P" ? 0.9 : 1.2;
      return resolution === "720P" ? 0.6 : 0.8;
    }
    return resolution === "720P" ? 0.9 : 1.2;
  }

  throw new Error(`Unsupported enterprise video model: ${input.model}`);
}

export function calculateEnterpriseVideoCredits(input: EnterpriseVideoPricingInput): number {
  const duration = Math.max(1, Math.ceil(Number(input.durationSeconds) || 1));
  return Number((getEnterpriseVideoCreditRate(input) * duration).toFixed(2));
}
