import { afterEach, describe, expect, it } from "vitest";

import { resolveBigModelBatchModel } from "../standardization/bigmodel-batch-client.ts";
import {
  resolveDocumentExtractionModel,
  resolveDocumentOcrModel,
} from "../standardization/document-extraction-llm.ts";
import { resolveSearchVisionModel } from "../playwright/search-vision-model.js";
import { resolveChatbotIntentModel } from "../../chatbot/src/llm-intent.js";
import { loadEnv, resetEnvCacheForTesting } from "./env.ts";
import { resolveLlmProvider, resolveStageLlmModel } from "./llm-client.ts";

const originalEnv = { ...process.env };
const minimalValidEnv: NodeJS.ProcessEnv = {
  SUPABASE_URL: "https://example.supabase.co",
  SUPABASE_SERVICE_ROLE_KEY: "service-role-key",
};

afterEach(() => {
  process.env = { ...originalEnv };
  resetEnvCacheForTesting();
});

describe("LLM stage regression matrix", () => {
  it("covers env fallback and DeepSeek provider detection", () => {
    const env = loadEnv({
      ...minimalValidEnv,
      LLM_MODEL: "global-model",
      STANDARDIZATION_LLM_MODEL: "standardization-stage-model",
    });

    expect(resolveStageLlmModel("standardization", { env })).toBe("standardization-stage-model");
    expect(resolveStageLlmModel("keyword_semantic", { env })).toBe("global-model");
    expect(resolveStageLlmModel("chatbot_intent", { env: { ...env, llmModel: undefined } })).toBe("deepseek-v4-flash");
    expect(resolveLlmProvider({
      provider: "env",
      baseURL: "https://api.deepseek.com",
      model: "deepseek-v4-pro",
    })).toBe("deepseek");
    expect(resolveLlmProvider({
      provider: "env",
      baseURL: "https://api.xiaomimimo.com/v1",
      model: "mimo-v2.5",
    })).toBe("mimo");
  });

  it("keeps the fixed search batch model on the supported whitelist", () => {
    expect(resolveBigModelBatchModel()).toBe("glm-4-plus");
    process.env.FIXED_SEARCH_LLM_MODEL = "glm-4-air";
    expect(resolveBigModelBatchModel()).toBe("glm-4-air");
    process.env.FIXED_SEARCH_LLM_MODEL = "unsupported-model";
    expect(() => resolveBigModelBatchModel()).toThrow(/FIXED_SEARCH_LLM_MODEL.*unsupported-model.*不受支持/);
  });

  it("keeps document OCR and extraction models independently configurable", () => {
    process.env = {
      ...originalEnv,
      ...minimalValidEnv,
      LLM_MODEL: "global-model",
      DOCUMENT_OCR_LLM_MODEL: "document-ocr-stage-model",
      DOCUMENT_EXTRACTION_LLM_MODEL: "document-extraction-stage-model",
    };
    resetEnvCacheForTesting();

    expect(resolveDocumentOcrModel()).toBe("document-ocr-stage-model");
    expect(resolveDocumentExtractionModel()).toBe("document-extraction-stage-model");
  });

  it("keeps search vision alias compatibility below the new stage env", () => {
    expect(resolveSearchVisionModel({
      SEARCH_VISION_LLM_MODEL: "search-stage-model",
      SEARCH_VISION_MODEL: "legacy-search-model",
      LLM_MODEL: "global-model",
    })).toBe("search-stage-model");
    expect(resolveSearchVisionModel({
      SEARCH_VISION_MODEL: "legacy-search-model",
      LLM_MODEL: "global-model",
    })).toBe("legacy-search-model");
  });

  it("keeps chatbot intent independent from automation stage models", () => {
    expect(resolveChatbotIntentModel({
      CHATBOT_INTENT_LLM_MODEL: "chatbot-intent-stage-model",
      STANDARDIZATION_LLM_MODEL: "standardization-stage-model",
      SEARCH_VISION_LLM_MODEL: "search-stage-model",
      LLM_MODEL: "global-model",
    })).toBe("chatbot-intent-stage-model");
  });
});
