import { describe, expect, it } from "vitest";
import { CALLBACK_SLOTS, callbackSchema, leadSchema } from "./lead-schema";

const goodLead = {
  name: "Jayveer Thakur",
  company: "Acme Manufacturing",
  email: "jay@acme.com",
  phone: "+919818993537",
  phoneCountry: "IN",
  industry: "Manufacturing",
  companySize: "21-100 employees",
  interests: ["Production", "Inventory"],
  notes: "Two plants, go-live in Q1.",
  consent: true as const,
  website: "",
};

const goodCallback = {
  name: "Jayveer Thakur",
  phone: "+919818993537",
  phoneCountry: "IN",
  email: "jay@acme.com",
  industry: "Manufacturing",
  slot: CALLBACK_SLOTS[0],
  consent: true as const,
  website: "",
};

describe("leadSchema", () => {
  it("accepts a complete valid lead", () => {
    expect(leadSchema.safeParse(goodLead).success).toBe(true);
  });

  it("rejects digits in name", () => {
    const r = leadSchema.safeParse({ ...goodLead, name: "Jay123" });
    expect(r.success).toBe(false);
    if (!r.success) {
      expect(r.error.issues[0].path[0]).toBe("name");
    }
  });

  it("rejects bad email", () => {
    expect(
      leadSchema.safeParse({ ...goodLead, email: "not-an-email" }).success
    ).toBe(false);
  });

  it("allows empty phone (optional) but validates when filled", () => {
    expect(
      leadSchema.safeParse({ ...goodLead, phone: "", phoneCountry: "" }).success
    ).toBe(true);
    const bad = leadSchema.safeParse({
      ...goodLead,
      phone: "+91123",
      phoneCountry: "IN",
    });
    expect(bad.success).toBe(false);
  });

  it("rejects 9-digit Indian numbers with the country message", () => {
    const r = leadSchema.safeParse({
      ...goodLead,
      phone: "+91981899353",
      phoneCountry: "IN",
    });
    expect(r.success).toBe(false);
    if (!r.success) {
      const phoneIssue = r.error.issues.find((i) => i.path[0] === "phone");
      expect(phoneIssue?.message).toMatch(/10 digits/);
    }
  });

  it("requires consent and blocks honeypot content", () => {
    expect(
      leadSchema.safeParse({ ...goodLead, consent: undefined }).success
    ).toBe(false);
    // honeypot filled still parses (action treats as bot, returns fake success)
    expect(
      leadSchema.safeParse({ ...goodLead, website: "x" }).success
    ).toBe(false);
  });

  it("rejects unknown industry", () => {
    expect(
      leadSchema.safeParse({ ...goodLead, industry: "Farming" }).success
    ).toBe(false);
  });
});

describe("callbackSchema", () => {
  it("accepts a complete valid callback", () => {
    expect(callbackSchema.safeParse(goodCallback).success).toBe(true);
  });

  it("requires phone (unlike demo leads)", () => {
    expect(
      callbackSchema.safeParse({ ...goodCallback, phone: "" }).success
    ).toBe(false);
  });

  it("rejects invalid slot", () => {
    expect(
      callbackSchema.safeParse({ ...goodCallback, slot: "Whenever" }).success
    ).toBe(false);
  });

  it("accepts empty optional email", () => {
    expect(
      callbackSchema.safeParse({ ...goodCallback, email: "" }).success
    ).toBe(true);
  });

  it("exposes the four time slots", () => {
    expect(CALLBACK_SLOTS).toHaveLength(4);
    expect(CALLBACK_SLOTS[0]).toBe("As soon as possible");
  });
});
