import { describe, expect, it } from "vitest";
import { buildTicketAccessUrl, buildTicketSmsPayload, isSuccessfulMnotifyResponse, isValidGhanaPhone, normalizeGhanaPhone } from "./mnotify";

describe("mNotify SMS helpers", () => {
  it("normalizes local and international Ghanaian numbers to local format", () => {
    expect(normalizeGhanaPhone("+233 55 225 0838")).toBe("0552250838");
    expect(normalizeGhanaPhone("233552250838")).toBe("0552250838");
    expect(normalizeGhanaPhone("0552250838")).toBe("0552250838");
  });

  it("validates Ghana mobile numbers", () => {
    expect(isValidGhanaPhone("+233552250838")).toBe(true);
    expect(isValidGhanaPhone("0552250838")).toBe(true);
    expect(isValidGhanaPhone("0241234567")).toBe(true);
    expect(isValidGhanaPhone("12345")).toBe(false);
  });

  it("builds the GNGBASH quick SMS payload with a direct browser ticket link", () => {
    const payload = buildTicketSmsPayload({ recipient: "+233552250838", attendeeName: "Ama", eventTitle: "Night Market", orderId: 42, quantity: 2, qrToken: "ticket-token-123456" });
    expect(payload).toEqual({
      recipient: ["0552250838"],
      sender: "GNGBASH",
      message: `Hi Ama, your gngtickets ticket for Night Market is confirmed. View your ticket: ${buildTicketAccessUrl("ticket-token-123456")}`,
      is_schedule: false,
      schedule_date: "",
    });
  });

  it("accepts mNotify's documented success response", () => {
    expect(isSuccessfulMnotifyResponse({ status: "success", code: "2000" })).toBe(true);
    expect(isSuccessfulMnotifyResponse({ status: "error", code: "4000" })).toBe(false);
    expect(isSuccessfulMnotifyResponse(null)).toBe(false);
  });

  it("supports the documented template placeholders", () => {
    expect(buildTicketSmsPayload({ recipient: "0552250838", attendeeName: "Ama", eventTitle: "Night Market", orderId: 42, quantity: 1, qrToken: "ticket-token-123456", template: "{name}|{event}|{order}|{link}" }).message).toContain("Ama|Night Market|42|https://");
  });
});
