import { describe, expect, it } from "../test/testHarness";

import {
  normalizeEnterpriseVideoPricingConfig,
  normalizePublicModelPrice,
  normalizePublicModelPrices,
  normalizePublicPricingPayload,
} from "./publicPricingClient";

describe("publicPricingClient", () => {
  it("normalizes camelCase public model price payloads", () => {
    expect(
      normalizePublicModelPrice({
        id: 1,
        modelKey: "gpt-4o",
        displayName: "GPT-4o",
        category: "text",
        pricingType: "token",
        inputPriceMills: 27,
        outputPriceMills: 108,
        flatPriceMills: null,
        currency: "CNY",
        enabled: true,
      }),
    ).toEqual({
      id: 1,
      modelKey: "gpt-4o",
      displayName: "GPT-4o",
      category: "text",
      pricingType: "token",
      inputPriceMills: 27,
      outputPriceMills: 108,
      flatPriceMills: null,
      currency: "CNY",
      enabled: true,
      createdAt: undefined,
      updatedAt: undefined,
    });
  });

  it("normalizes snake_case public model price payloads inside containers", () => {
    expect(
      normalizePublicModelPrices({
        prices: [
          {
            model_key: "deepseek-chat",
            display_name: "DeepSeek Chat",
            pricing_type: "token",
            input_price_mills: "2",
            output_price_mills: "8",
            flat_price_mills: "0",
            enabled: 1,
          },
          { display_name: "missing key" },
        ],
      }),
    ).toEqual([
      {
        id: undefined,
        modelKey: "deepseek-chat",
        displayName: "DeepSeek Chat",
        category: undefined,
        pricingType: "token",
        inputPriceMills: 2,
        outputPriceMills: 8,
        flatPriceMills: 0,
        currency: "CNY",
        enabled: true,
        createdAt: undefined,
        updatedAt: undefined,
      },
    ]);
  });

  it("normalizes public pricing payloads with model prices and enterprise video pricing", () => {
    expect(
      normalizePublicPricingPayload({
        modelPrices: [
          {
            modelKey: "qwen-turbo",
            pricingType: "token",
            inputPriceMills: 2,
            outputPriceMills: 6,
          },
        ],
        enterpriseVideoPricing: {
          currency: "CNY",
          creditsPerCny: 100,
          billingUnit: "per_second",
          defaultResolution: "1080P",
          resolutions: ["720P", "1080P"],
          rules: [
            {
              id: "happyhorse",
              modelIncludes: ["happyhorse"],
              rates: { "720P": 0.72, "1080P": 1.28 },
            },
          ],
        },
      }),
    ).toEqual({
      modelPrices: [
        {
          id: undefined,
          modelKey: "qwen-turbo",
          displayName: undefined,
          category: undefined,
          pricingType: "token",
          inputPriceMills: 2,
          outputPriceMills: 6,
          flatPriceMills: null,
          currency: "CNY",
          enabled: true,
          createdAt: undefined,
          updatedAt: undefined,
        },
      ],
      enterpriseVideoPricing: {
        currency: "CNY",
        creditsPerCny: 100,
        billingUnit: "per_second",
        defaultResolution: "1080P",
        resolutions: ["720P", "1080P"],
        rules: [
          {
            id: "happyhorse",
            modelIncludes: ["happyhorse"],
            rates: { "720P": 0.72, "1080P": 1.28 },
          },
        ],
      },
    });
  });

  it("rejects malformed enterprise video pricing configs", () => {
    expect(
      normalizeEnterpriseVideoPricingConfig({
        rules: [{ id: "broken", modelIncludes: [], rates: {} }],
      }),
    ).toEqual(null);
  });
});
