import { describe, it, expect, vi, beforeEach } from "vitest";

// Mock AI SDK providers
const mockOpenAiModel = vi.fn(() => "openai-model-instance");
const mockAnthropicModel = vi.fn(() => "anthropic-model-instance");
const mockOpenAiProvider = vi.fn(() => mockOpenAiModel);
const mockAnthropicProvider = vi.fn(() => mockAnthropicModel);

vi.mock("@ai-sdk/openai", () => ({
  createOpenAI: vi.fn(() => mockOpenAiProvider),
}));

vi.mock("@ai-sdk/anthropic", () => ({
  createAnthropic: vi.fn(() => mockAnthropicProvider),
}));

const { createOpenAI } = await import("@ai-sdk/openai");
const { createAnthropic } = await import("@ai-sdk/anthropic");
const { resolveModel } = await import("../factory");

beforeEach(() => {
  vi.clearAllMocks();
  delete process.env["OPENAI_API_KEY"];
  delete process.env["ANTHROPIC_API_KEY"];
  delete process.env["OPENROUTER_API_KEY"];
  delete process.env["OLLAMA_BASE_URL"];
});

describe("resolveModel", () => {
  it("uses openai provider when specified", () => {
    resolveModel({ provider: "openai", apiKey: "sk-test" });
    expect(createOpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "sk-test" }));
  });

  it("uses anthropic provider when specified", () => {
    resolveModel({ provider: "anthropic", apiKey: "sk-ant-test" });
    expect(createAnthropic).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "sk-ant-test" }));
  });

  it("uses openrouter base URL when provider is openrouter", () => {
    resolveModel({ provider: "openrouter", apiKey: "or-key" });
    expect(createOpenAI).toHaveBeenCalledWith(
      expect.objectContaining({ baseURL: "https://openrouter.ai/api/v1" })
    );
  });

  it("uses ollama base URL from env or default", () => {
    process.env["OLLAMA_BASE_URL"] = "http://custom:11434/v1";
    resolveModel({ provider: "ollama" });
    expect(createOpenAI).toHaveBeenCalledWith(
      expect.objectContaining({ baseURL: "http://custom:11434/v1" })
    );
  });

  it("falls back to ollama default URL when env not set", () => {
    resolveModel({ provider: "ollama" });
    expect(createOpenAI).toHaveBeenCalledWith(
      expect.objectContaining({ baseURL: "http://localhost:11434/v1" })
    );
  });

  it("uses custom model when specified", () => {
    resolveModel({ provider: "openai", model: "gpt-4-turbo", apiKey: "sk-test" });
    expect(mockOpenAiProvider).toHaveBeenCalledWith("gpt-4-turbo");
  });

  it("uses default openai model when none specified", () => {
    resolveModel({ provider: "openai", apiKey: "sk-test" });
    expect(mockOpenAiProvider).toHaveBeenCalledWith("gpt-4o-mini");
  });

  it("uses default anthropic model when none specified", () => {
    resolveModel({ provider: "anthropic", apiKey: "sk-ant" });
    expect(mockAnthropicProvider).toHaveBeenCalledWith("claude-haiku-4-5-20251001");
  });

  it("defaults to openrouter when OPENROUTER_API_KEY is set (no explicit provider)", () => {
    process.env["OPENROUTER_API_KEY"] = "or-key";
    resolveModel({});
    expect(createOpenAI).toHaveBeenCalledWith(
      expect.objectContaining({ baseURL: "https://openrouter.ai/api/v1" })
    );
  });

  it("defaults to openai when OPENAI_API_KEY set and no openrouter", () => {
    process.env["OPENAI_API_KEY"] = "sk-test";
    resolveModel({});
    expect(createOpenAI).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "sk-test" }));
  });

  it("throws on unknown provider", () => {
    // @ts-expect-error - testing invalid input
    expect(() => resolveModel({ provider: "nonexistent" })).toThrow();
  });
});
