import { describe, expect, it } from "vitest";
import { getProviderConfigFromEnv } from "../src/provider-config-env.ts";

describe("provider config env", () => {
  it("reads google config from canonical env keys", () => {
    expect(
      getProviderConfigFromEnv({
        BEST_AGENT_PROVIDER_KIND: "google",
        BEST_AGENT_PROVIDER_MODEL: "gemini-2.5-flash",
        BEST_AGENT_PROVIDER_BASE_URL: "https://google.example/v1",
        GOOGLE_GENERATIVE_AI_API_KEY: "google-key",
      }),
    ).toEqual({
      kind: "google",
      model: "gemini-2.5-flash",
      apiKey: "google-key",
      baseURL: "https://google.example/v1",
      compatibilityMode: undefined,
      systemPrompt: undefined,
      timeoutMs: undefined,
    });
  });

  it("reads anthropic config from canonical env keys", () => {
    expect(
      getProviderConfigFromEnv({
        BEST_AGENT_PROVIDER_KIND: "anthropic",
        BEST_AGENT_PROVIDER_MODEL: "claude-sonnet-4-5",
        BEST_AGENT_PROVIDER_BASE_URL: "https://anthropic.example",
        ANTHROPIC_API_KEY: "anthropic-key",
      }),
    ).toEqual({
      kind: "anthropic",
      model: "claude-sonnet-4-5",
      apiKey: "anthropic-key",
      baseURL: "https://anthropic.example",
      compatibilityMode: undefined,
      systemPrompt: undefined,
      timeoutMs: undefined,
    });
  });

  it("ignores removed gemini aliases", () => {
    expect(
      getProviderConfigFromEnv({
        BEST_AGENT_PROVIDER_KIND: "google",
        BEST_AGENT_PROVIDER_MODEL: "gemini-2.5-flash",
        GEMINI_API_KEY: "legacy-google-key",
        GEMINI_BASE_URL: "https://legacy-google.example/v1",
        GOOGLE_BASE_URL: "https://legacy-google.example/v1",
      }),
    ).toEqual({
      kind: "google",
      model: "gemini-2.5-flash",
      apiKey: undefined,
      baseURL: undefined,
      compatibilityMode: undefined,
      systemPrompt: undefined,
      timeoutMs: undefined,
    });
  });

  it("ignores removed anthropic base URL aliases", () => {
    expect(
      getProviderConfigFromEnv({
        BEST_AGENT_PROVIDER_KIND: "anthropic",
        BEST_AGENT_PROVIDER_MODEL: "claude-sonnet-4-5",
        ANTHROPIC_API_KEY: "anthropic-key",
        ANTHROPIC_BASE_URL: "https://legacy-anthropic.example",
      }),
    ).toEqual({
      kind: "anthropic",
      model: "claude-sonnet-4-5",
      apiKey: "anthropic-key",
      baseURL: undefined,
      compatibilityMode: undefined,
      systemPrompt: undefined,
      timeoutMs: undefined,
    });
  });

  it("infers moonshot config from canonical env keys", () => {
    expect(
      getProviderConfigFromEnv({
        BEST_AGENT_PROVIDER_COMPATIBILITY_MODE: "compatible",
        MOONSHOTAI_API_KEY: "moonshot-key",
        MOONSHOTAI_BASE_URL: "https://moonshot.example/v1",
        MOONSHOTAI_MODEL: "moonshot-v1",
      }),
    ).toEqual({
      kind: "moonshotai",
      model: "moonshot-v1",
      apiKey: "moonshot-key",
      baseURL: "https://moonshot.example/v1",
      compatibilityMode: "compatible",
      systemPrompt: undefined,
      timeoutMs: undefined,
    });
  });

  it("ignores removed provider-specific compatibility mode envs", () => {
    const cases = [
      {
        kind: "xai",
        modelEnv: { XAI_MODEL: "grok-4" },
        apiEnv: { XAI_API_KEY: "xai-key" },
        compatibilityEnv: { XAI_COMPATIBILITY_MODE: "compatible" },
      },
      {
        kind: "deepseek",
        modelEnv: { DEEPSEEK_MODEL: "deepseek-chat" },
        apiEnv: { DEEPSEEK_API_KEY: "deepseek-key" },
        compatibilityEnv: { DEEPSEEK_COMPATIBILITY_MODE: "compatible" },
      },
      {
        kind: "moonshotai",
        modelEnv: { MOONSHOTAI_MODEL: "moonshot-v1" },
        apiEnv: { MOONSHOTAI_API_KEY: "moonshot-key" },
        compatibilityEnv: { MOONSHOTAI_COMPATIBILITY_MODE: "compatible" },
      },
      {
        kind: "aihubmix",
        modelEnv: { AIHUBMIX_MODEL: "aihubmix-chat" },
        apiEnv: { AIHUBMIX_API_KEY: "aihubmix-key" },
        compatibilityEnv: { AIHUBMIX_COMPATIBILITY_MODE: "compatible" },
      },
      {
        kind: "zenmux",
        modelEnv: { ZENMUX_MODEL: "zenmux-chat" },
        apiEnv: { ZENMUX_API_KEY: "zenmux-key" },
        compatibilityEnv: { ZENMUX_COMPATIBILITY_MODE: "compatible" },
      },
      {
        kind: "customProvider",
        modelEnv: { CUSTOM_PROVIDER_MODEL: "custom-model" },
        apiEnv: { CUSTOM_PROVIDER_API_KEY: "custom-key" },
        compatibilityEnv: { CUSTOM_PROVIDER_COMPATIBILITY_MODE: "compatible" },
      },
    ] as const;

    for (const testCase of cases) {
      expect(
        getProviderConfigFromEnv({
          ...testCase.apiEnv,
          ...testCase.modelEnv,
          ...testCase.compatibilityEnv,
        }),
      ).toMatchObject({
        kind: testCase.kind,
        compatibilityMode: undefined,
      });

      expect(
        getProviderConfigFromEnv({
          ...testCase.apiEnv,
          ...testCase.modelEnv,
          BEST_AGENT_PROVIDER_COMPATIBILITY_MODE: "compatible",
        }),
      ).toMatchObject({
        kind: testCase.kind,
        compatibilityMode: "compatible",
      });
    }
  });

  it("ignores removed moonshot aliases", () => {
    expect(
      getProviderConfigFromEnv({
        BEST_AGENT_PROVIDER_KIND: "moonshotai",
        BEST_AGENT_PROVIDER_MODEL: "moonshot-v1",
        MOONSHOT_API_KEY: "legacy-moonshot-key",
        MOONSHOT_BASE_URL: "https://legacy-moonshot.example/v1",
        MOONSHOT_COMPATIBILITY_MODE: "native",
      }),
    ).toEqual({
      kind: "moonshotai",
      model: "moonshot-v1",
      apiKey: undefined,
      baseURL: undefined,
      compatibilityMode: undefined,
      systemPrompt: undefined,
      timeoutMs: undefined,
    });

    expect(
      getProviderConfigFromEnv({
        MOONSHOT_MODEL: "legacy-moonshot-v1",
      }),
    ).toBeUndefined();
  });
});
