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

import { UnlockPrivateResponseSchema } from "@/data/schemas/chat";

describe("UnlockPrivateResponse", () => {
  it("parses a successful single-message unlock response", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: true,
      content: "完整消息内容",
      audioUrl: "https://example.com/unlocked-voice.mp3",
      reason: "ok",
      creditBalance: 9180,
      creditsCharged: 10,
      requiredCredits: 10,
      shortfallCredits: 0,
    });

    expect(response.unlocked).toBe(true);
    expect(response.content).toBe("完整消息内容");
    expect(response.audioUrl).toBe("https://example.com/unlocked-voice.mp3");
    expect(response.reason).toBe("ok");
    expect(response.creditBalance).toBe(9180);
    expect(response.creditsCharged).toBe(10);
    expect(response.requiredCredits).toBe(10);
    expect(response.shortfallCredits).toBe(0);
  });

  it("parses canonical ids, audio, and unlocked images", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: true,
      messageId: "backend-message-1",
      clientLockId: "promotion-1",
      lockType: "image_paywall",
      content: "Unlocked reply",
      audioUrl: "https://example.com/audio.mp3",
      image: {
        type: "promotion",
        url: "https://example.com/image.jpg",
      },
    });

    expect(response.messageId).toBe("backend-message-1");
    expect(response.content).toBe("Unlocked reply");
    expect(response.audioUrl).toBe("https://example.com/audio.mp3");
    expect(response.image.url).toBe("https://example.com/image.jpg");
    expect(response).toMatchObject({
      clientLockId: "promotion-1",
      lockType: "image_paywall",
    });
  });

  it("parses an insufficient-credits unlock response", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: false,
      content: "",
      reason: "insufficient_credits",
      creditBalance: 5,
      creditsCharged: 0,
      requiredCredits: 10,
      shortfallCredits: 5,
    });

    expect(response.unlocked).toBe(false);
    expect(response.reason).toBe("insufficient_credits");
    expect(response.creditBalance).toBe(5);
    expect(response.creditsCharged).toBe(0);
    expect(response.requiredCredits).toBe(10);
    expect(response.shortfallCredits).toBe(5);
  });

  it("defaults nullable content to an empty string", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: false,
      content: null,
      audioUrl: null,
      reason: "insufficient_credits",
    });

    expect(response.content).toBe("");
    expect(response.audioUrl).toBe("");
  });

  it("defaults nullable credit fields to zero", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: false,
      content: "",
      reason: "insufficient_credits",
      creditBalance: null,
      creditsCharged: null,
      requiredCredits: null,
      shortfallCredits: null,
    });

    expect(response.creditBalance).toBe(0);
    expect(response.creditsCharged).toBe(0);
    expect(response.requiredCredits).toBe(0);
    expect(response.shortfallCredits).toBe(0);
  });

  it("strips legacy lock detail fields", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: false,
      lockDetail: {
        locked: true,
        reason: "private_message",
        showContent: false,
        showUpgrade: true,
        hint: "Legacy hint",
        detail: { messageId: "legacy-message" },
      },
    });

    expect(response).not.toHaveProperty("lockDetail");
    expect(response).not.toHaveProperty("lockDetail");
  });

  it("does not fall back to removed response aliases", () => {
    const response = UnlockPrivateResponseSchema.parse({
      unlocked: true,
      message_id: "legacy-message",
      reply: "Legacy reply",
      audio_url: "https://example.com/legacy.mp3",
    });

    expect(response.messageId).toBe("");
    expect(response.content).toBe("");
    expect(response.audioUrl).toBe("");
    expect(response).not.toHaveProperty("message_id");
    expect(response).not.toHaveProperty("reply");
    expect(response).not.toHaveProperty("audio_url");
  });
});
