import { describe, expect, it } from "vitest";
import { migrateLegacyCustomProvider } from "../src/dimsdk/compat/archships/dim-agent/config/index.ts";
import type { DimcodeUserConfigV1 } from "../src/dimsdk/compat/archships/dim-agent/config/user-store.ts";

describe("provider migration", () => {
  it("migrates legacy bailian-coding-plan baseUrl to OpenAI-compatible endpoint", () => {
    const config: DimcodeUserConfigV1 = {
      version: 1,
      updatedAt: 0,
      settings: {
        providerConnections: {
          "bailian-coding-plan": {
            apiKey: "sk-test",
            baseUrl: "https://coding.dashscope.aliyuncs.com/apps/anthropic/v1",
          },
        },
      },
    };

    const result = migrateLegacyCustomProvider(config);

    expect(result.migrated).toBe(true);
    expect(config.settings?.providerConnections?.["bailian-coding-plan"]?.baseUrl).toBe(
      "https://coding.dashscope.aliyuncs.com/v1",
    );
    expect(config.settings?.providerConnections?.["bailian-coding-plan"]?.apiKey).toBe("sk-test");
  });

  it("does not rewrite non-target provider or non-legacy URL", () => {
    const config: DimcodeUserConfigV1 = {
      version: 1,
      updatedAt: 0,
      settings: {
        providerConnections: {
          openai: {
            baseUrl: "https://coding.dashscope.aliyuncs.com/apps/anthropic/v1",
          },
          "bailian-coding-plan": {
            baseUrl: "https://coding.dashscope.aliyuncs.com/v1",
          },
        },
      },
    };

    const result = migrateLegacyCustomProvider(config);

    expect(result.migrated).toBe(false);
    expect(config.settings?.providerConnections?.openai?.baseUrl).toBe(
      "https://coding.dashscope.aliyuncs.com/apps/anthropic/v1",
    );
    expect(config.settings?.providerConnections?.["bailian-coding-plan"]?.baseUrl).toBe(
      "https://coding.dashscope.aliyuncs.com/v1",
    );
  });

  it("migrates legacy custom provider state into customProviders and rewrites the active provider id", () => {
    const config: DimcodeUserConfigV1 = {
      version: 1,
      updatedAt: 0,
      settings: {
        providerId: "custom",
        activeModelByProvider: {
          custom: "gpt-4.1-mini",
        },
        customProvider: {
          apiKey: "sk-legacy",
          baseUrl: "https://proxy.example/v1",
          models: [{ id: "gpt-4.1-mini", name: "GPT 4.1 Mini" }],
        },
      },
    };

    const result = migrateLegacyCustomProvider(config);

    expect(result.migrated).toBe(true);
    expect(result.customProviders).toHaveLength(1);
    expect(result.customProviders[0]).toMatchObject({
      adapter: "openai",
      apiKey: "sk-legacy",
      baseUrl: "https://proxy.example/v1",
      models: [{ id: "gpt-4.1-mini", name: "GPT 4.1 Mini" }],
      name: "Custom",
    });
    expect(result.customProviders[0]?.id).toMatch(/^cp_/);
    expect(config.settings?.providerId).toBe(result.customProviders[0]?.id);
    expect(config.settings?.activeModelByProvider).toEqual({
      [result.customProviders[0]!.id]: "gpt-4.1-mini",
    });
  });
});
