/* eslint-disable no-console */
import {
  CostClass,
  CostConfidence,
  DomainTag,
  LatencyClass,
  ModalityKind,
  ModelLifecycle,
  PrismaClient,
  PrivacyClass,
  QualityTier,
} from '../src/generated/prisma';

const prisma = new PrismaClient();

type SeedProfile = {
  provider: string;
  modelKey: string;
  displayName: string;
  family?: string;
  isLocal?: boolean;
  isRouterOnly?: boolean;
  modalitiesIn?: ModalityKind[];
  modalitiesOut?: ModalityKind[];
  contextWindowTokens?: number;
  maxOutputTokens?: number;
  domainTags?: DomainTag[];
  notRecommendedFor?: DomainTag[];
  inputCostPer1M?: number;
  outputCostPer1M?: number;
  costConfidence?: CostConfidence;
  costClass?: CostClass;
  latencyP95Ms?: number;
  latencyClass?: LatencyClass;
  qualityTier?: QualityTier;
  judgeSuitability?: boolean;
  searchSuitability?: boolean;
  fallbackSuitability?: boolean;
  privacySupport?: PrivacyClass;
};

const TEXT_BOTH: ModalityKind[] = [ModalityKind.TEXT];
const TEXT_AND_VISION: ModalityKind[] = [
  ModalityKind.TEXT,
  ModalityKind.IMAGE_INPUT,
  ModalityKind.PDF_INPUT,
];

const PROFILES: SeedProfile[] = [
  // ─── OpenAI (cloud) ──────────────────────────────────────
  {
    provider: 'OPENAI',
    modelKey: 'gpt-4o',
    displayName: 'GPT-4o',
    family: 'gpt-4o',
    modalitiesIn: TEXT_AND_VISION,
    modalitiesOut: [ModalityKind.TEXT, ModalityKind.STRUCTURED_OUTPUT],
    contextWindowTokens: 128_000,
    maxOutputTokens: 16_384,
    domainTags: [DomainTag.CODING, DomainTag.GENERAL, DomainTag.MARKETING, DomainTag.EDUCATION],
    inputCostPer1M: 2.5,
    outputCostPer1M: 10,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 1800,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.S,
    judgeSuitability: true,
  },
  {
    provider: 'OPENAI',
    modelKey: 'gpt-4o-mini',
    displayName: 'GPT-4o Mini',
    family: 'gpt-4o',
    modalitiesIn: TEXT_AND_VISION,
    modalitiesOut: [ModalityKind.TEXT, ModalityKind.STRUCTURED_OUTPUT],
    contextWindowTokens: 128_000,
    maxOutputTokens: 16_384,
    domainTags: [DomainTag.GENERAL, DomainTag.CREATIVE_WRITING, DomainTag.MARKETING],
    inputCostPer1M: 0.15,
    outputCostPer1M: 0.6,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.CHEAP,
    latencyP95Ms: 900,
    latencyClass: LatencyClass.FAST,
    qualityTier: QualityTier.A,
  },
  {
    provider: 'OPENAI',
    modelKey: 'o1',
    displayName: 'OpenAI o1',
    family: 'o-series',
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 200_000,
    maxOutputTokens: 100_000,
    domainTags: [DomainTag.RESEARCH, DomainTag.PHYSICS, DomainTag.CODING],
    inputCostPer1M: 15,
    outputCostPer1M: 60,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.ULTRA,
    latencyP95Ms: 12_000,
    latencyClass: LatencyClass.HEAVY,
    qualityTier: QualityTier.S,
    judgeSuitability: true,
  },
  {
    provider: 'OPENAI',
    modelKey: 'o1-mini',
    displayName: 'OpenAI o1 Mini',
    family: 'o-series',
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 128_000,
    maxOutputTokens: 65_536,
    domainTags: [DomainTag.CODING, DomainTag.RESEARCH],
    inputCostPer1M: 3,
    outputCostPer1M: 12,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.PREMIUM,
    latencyP95Ms: 5000,
    latencyClass: LatencyClass.SLOW,
    qualityTier: QualityTier.A,
  },

  // ─── Anthropic (cloud) ───────────────────────────────────
  {
    provider: 'ANTHROPIC',
    modelKey: 'claude-opus-4',
    displayName: 'Claude Opus 4',
    family: 'claude-4',
    modalitiesIn: TEXT_AND_VISION,
    modalitiesOut: [ModalityKind.TEXT, ModalityKind.STRUCTURED_OUTPUT],
    contextWindowTokens: 200_000,
    maxOutputTokens: 16_384,
    domainTags: [DomainTag.CODING, DomainTag.LEGAL, DomainTag.RESEARCH, DomainTag.EDUCATION],
    inputCostPer1M: 15,
    outputCostPer1M: 75,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.ULTRA,
    latencyP95Ms: 2200,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.S,
    judgeSuitability: true,
  },
  {
    provider: 'ANTHROPIC',
    modelKey: 'claude-sonnet-4',
    displayName: 'Claude Sonnet 4',
    family: 'claude-4',
    modalitiesIn: TEXT_AND_VISION,
    modalitiesOut: [ModalityKind.TEXT, ModalityKind.STRUCTURED_OUTPUT],
    contextWindowTokens: 200_000,
    maxOutputTokens: 16_384,
    domainTags: [
      DomainTag.CODING,
      DomainTag.GENERAL,
      DomainTag.MARKETING,
      DomainTag.CREATIVE_WRITING,
    ],
    inputCostPer1M: 3,
    outputCostPer1M: 15,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 1500,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.S,
    judgeSuitability: true,
  },
  {
    provider: 'ANTHROPIC',
    modelKey: 'claude-haiku-4',
    displayName: 'Claude Haiku 4',
    family: 'claude-4',
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 200_000,
    maxOutputTokens: 8192,
    domainTags: [DomainTag.GENERAL, DomainTag.TRANSLATION, DomainTag.MARKETING],
    inputCostPer1M: 0.8,
    outputCostPer1M: 4,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 700,
    latencyClass: LatencyClass.FAST,
    qualityTier: QualityTier.A,
  },

  // ─── Google Gemini (cloud) ───────────────────────────────
  {
    provider: 'GEMINI',
    modelKey: 'gemini-2.5-pro',
    displayName: 'Gemini 2.5 Pro',
    family: 'gemini-2.5',
    modalitiesIn: [
      ModalityKind.TEXT,
      ModalityKind.IMAGE_INPUT,
      ModalityKind.PDF_INPUT,
      ModalityKind.VIDEO_INPUT,
      ModalityKind.AUDIO_INPUT,
      ModalityKind.YOUTUBE_INPUT,
    ],
    modalitiesOut: [ModalityKind.TEXT, ModalityKind.STRUCTURED_OUTPUT],
    contextWindowTokens: 2_000_000,
    maxOutputTokens: 65_536,
    domainTags: [DomainTag.RESEARCH, DomainTag.MULTIMEDIA, DomainTag.CODING, DomainTag.BIOLOGY],
    inputCostPer1M: 1.25,
    outputCostPer1M: 10,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 2500,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.S,
    searchSuitability: true,
  },
  {
    provider: 'GEMINI',
    modelKey: 'gemini-2.5-flash',
    displayName: 'Gemini 2.5 Flash',
    family: 'gemini-2.5',
    modalitiesIn: [
      ModalityKind.TEXT,
      ModalityKind.IMAGE_INPUT,
      ModalityKind.PDF_INPUT,
      ModalityKind.VIDEO_INPUT,
      ModalityKind.AUDIO_INPUT,
      ModalityKind.YOUTUBE_INPUT,
    ],
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 1_000_000,
    maxOutputTokens: 8192,
    domainTags: [DomainTag.GENERAL, DomainTag.MULTIMEDIA, DomainTag.TRANSLATION],
    inputCostPer1M: 0.075,
    outputCostPer1M: 0.3,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.CHEAP,
    latencyP95Ms: 700,
    latencyClass: LatencyClass.FAST,
    qualityTier: QualityTier.A,
    searchSuitability: true,
  },

  // ─── DeepSeek (cloud) ────────────────────────────────────
  {
    provider: 'DEEPSEEK',
    modelKey: 'deepseek-chat',
    displayName: 'DeepSeek V3',
    family: 'deepseek-v3',
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 64_000,
    maxOutputTokens: 8192,
    domainTags: [DomainTag.CODING, DomainTag.RESEARCH, DomainTag.GENERAL],
    inputCostPer1M: 0.14,
    outputCostPer1M: 0.28,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.CHEAP,
    latencyP95Ms: 1500,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.A,
  },
  {
    provider: 'DEEPSEEK',
    modelKey: 'deepseek-reasoner',
    displayName: 'DeepSeek R1',
    family: 'deepseek-r1',
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 64_000,
    maxOutputTokens: 8192,
    domainTags: [DomainTag.RESEARCH, DomainTag.PHYSICS, DomainTag.CODING],
    inputCostPer1M: 0.55,
    outputCostPer1M: 2.19,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 8000,
    latencyClass: LatencyClass.SLOW,
    qualityTier: QualityTier.S,
  },

  // ─── Grok (cloud) ────────────────────────────────────────
  {
    provider: 'GROK',
    modelKey: 'grok-3',
    displayName: 'Grok 3',
    family: 'grok-3',
    modalitiesIn: TEXT_AND_VISION,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 131_072,
    maxOutputTokens: 8192,
    domainTags: [DomainTag.GENERAL, DomainTag.CREATIVE_WRITING, DomainTag.RESEARCH],
    inputCostPer1M: 3,
    outputCostPer1M: 15,
    costConfidence: CostConfidence.ESTIMATED,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 2000,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.A,
  },

  // ─── AWS Bedrock (cloud) ─────────────────────────────────
  {
    provider: 'BEDROCK',
    modelKey: 'anthropic.claude-sonnet-4-v1:0',
    displayName: 'Bedrock — Claude Sonnet 4',
    family: 'claude-4',
    modalitiesIn: TEXT_AND_VISION,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 200_000,
    maxOutputTokens: 16_384,
    domainTags: [DomainTag.CODING, DomainTag.GENERAL, DomainTag.LEGAL],
    inputCostPer1M: 3.3,
    outputCostPer1M: 16.5,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.STANDARD,
    latencyP95Ms: 1900,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.S,
  },

  // ─── Ollama (local) ──────────────────────────────────────
  {
    provider: 'OLLAMA',
    modelKey: 'qwen3:1.7b',
    displayName: 'Qwen 3 1.7B (router)',
    family: 'qwen3',
    isLocal: true,
    isRouterOnly: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 32_768,
    domainTags: [DomainTag.GENERAL],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 600,
    latencyClass: LatencyClass.FAST,
    qualityTier: QualityTier.B,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'OLLAMA',
    modelKey: 'gemma3:4b',
    displayName: 'Gemma 3 4B',
    family: 'gemma-3',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 8192,
    domainTags: [DomainTag.GENERAL, DomainTag.TRANSLATION],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 1500,
    latencyClass: LatencyClass.MEDIUM,
    qualityTier: QualityTier.B,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
    fallbackSuitability: true,
  },
  {
    provider: 'OLLAMA',
    modelKey: 'qwen2.5-coder:32b',
    displayName: 'Qwen 2.5 Coder 32B',
    family: 'qwen2.5-coder',
    isLocal: true,
    modalitiesIn: [ModalityKind.TEXT, ModalityKind.CODE],
    modalitiesOut: [ModalityKind.TEXT, ModalityKind.CODE, ModalityKind.STRUCTURED_OUTPUT],
    contextWindowTokens: 131_072,
    domainTags: [DomainTag.CODING],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 3500,
    latencyClass: LatencyClass.SLOW,
    qualityTier: QualityTier.A,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'OLLAMA',
    modelKey: 'deepseek-r1:32b',
    displayName: 'DeepSeek R1 32B (local)',
    family: 'deepseek-r1',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 131_072,
    domainTags: [DomainTag.RESEARCH, DomainTag.PHYSICS, DomainTag.CODING],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 6000,
    latencyClass: LatencyClass.SLOW,
    qualityTier: QualityTier.A,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'OLLAMA',
    modelKey: 'medgemma:27b',
    displayName: 'MedGemma 27B',
    family: 'medgemma',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 8192,
    domainTags: [DomainTag.MEDICAL, DomainTag.MENTAL_HEALTH],
    notRecommendedFor: [DomainTag.CODING, DomainTag.CREATIVE_WRITING],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 4500,
    latencyClass: LatencyClass.SLOW,
    qualityTier: QualityTier.A,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'OLLAMA',
    modelKey: 'llama3.3:70b',
    displayName: 'Llama 3.3 70B',
    family: 'llama-3.3',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 128_000,
    domainTags: [DomainTag.GENERAL, DomainTag.CREATIVE_WRITING, DomainTag.RESEARCH],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 7000,
    latencyClass: LatencyClass.SLOW,
    qualityTier: QualityTier.A,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'OLLAMA',
    modelKey: 'phi-4-mini',
    displayName: 'Phi-4 Mini (router)',
    family: 'phi-4',
    isLocal: true,
    isRouterOnly: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 16_384,
    domainTags: [DomainTag.GENERAL],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 800,
    latencyClass: LatencyClass.FAST,
    qualityTier: QualityTier.B,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },

  // ─── LlamaCpp (frontier local) ───────────────────────────
  {
    provider: 'LLAMACPP',
    modelKey: 'kimi-k2',
    displayName: 'Kimi K2 (llama.cpp)',
    family: 'kimi',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 200_000,
    domainTags: [DomainTag.RESEARCH, DomainTag.CODING, DomainTag.MARKETING],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 10_000,
    latencyClass: LatencyClass.HEAVY,
    qualityTier: QualityTier.S,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'LLAMACPP',
    modelKey: 'glm-5.1',
    displayName: 'GLM-5.1 (llama.cpp)',
    family: 'glm-5',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 200_000,
    domainTags: [DomainTag.RESEARCH, DomainTag.GENERAL],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 12_000,
    latencyClass: LatencyClass.HEAVY,
    qualityTier: QualityTier.S,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
  {
    provider: 'LLAMACPP',
    modelKey: 'deepseek-v3.2',
    displayName: 'DeepSeek V3.2 (llama.cpp)',
    family: 'deepseek-v3',
    isLocal: true,
    modalitiesIn: TEXT_BOTH,
    modalitiesOut: TEXT_BOTH,
    contextWindowTokens: 128_000,
    domainTags: [DomainTag.CODING, DomainTag.RESEARCH],
    inputCostPer1M: 0,
    outputCostPer1M: 0,
    costConfidence: CostConfidence.EXACT,
    costClass: CostClass.FREE,
    latencyP95Ms: 9000,
    latencyClass: LatencyClass.HEAVY,
    qualityTier: QualityTier.S,
    privacySupport: PrivacyClass.LOCAL_PREFERRED,
  },
];

async function main(): Promise<void> {
  console.log(`Seeding ${PROFILES.length} RouterModelRegistry profiles...`);
  for (const profile of PROFILES) {
    await prisma.routerModelRegistry.upsert({
      where: { provider_modelKey: { provider: profile.provider, modelKey: profile.modelKey } },
      create: {
        provider: profile.provider,
        modelKey: profile.modelKey,
        displayName: profile.displayName,
        family: profile.family ?? null,
        isLocal: profile.isLocal ?? false,
        isRouterOnly: profile.isRouterOnly ?? false,
        isExecutionCapable: !(profile.isRouterOnly ?? false),
        lifecycle: ModelLifecycle.ACTIVE,
        modalitiesIn: profile.modalitiesIn ?? TEXT_BOTH,
        modalitiesOut: profile.modalitiesOut ?? TEXT_BOTH,
        contextWindowTokens: profile.contextWindowTokens ?? null,
        maxOutputTokens: profile.maxOutputTokens ?? null,
        domainTags: profile.domainTags ?? [DomainTag.GENERAL],
        notRecommendedFor: profile.notRecommendedFor ?? [],
        inputCostPer1M: profile.inputCostPer1M ?? null,
        outputCostPer1M: profile.outputCostPer1M ?? null,
        costConfidence: profile.costConfidence ?? CostConfidence.UNKNOWN,
        costClass: profile.costClass ?? null,
        latencyP95Ms: profile.latencyP95Ms ?? null,
        latencyClass: profile.latencyClass ?? null,
        qualityTier: profile.qualityTier ?? QualityTier.B,
        judgeSuitability: profile.judgeSuitability ?? false,
        searchSuitability: profile.searchSuitability ?? false,
        fallbackSuitability: profile.fallbackSuitability ?? true,
        privacySupport: profile.privacySupport ?? PrivacyClass.CLOUD_PERMITTED,
        metadataSource: 'seed',
      },
      update: {
        displayName: profile.displayName,
      },
    });
  }
  const count = await prisma.routerModelRegistry.count();
  console.log(`Seed complete. Registry contains ${count} profiles.`);
}

main()
  .catch((err) => {
    console.error(err);
    process.exit(1);
  })
  .finally(() => {
    void prisma.$disconnect();
  });
