import { describe, it, expect } from "vitest";
import {
  CreateCustomerSchema,
  UpdateCustomerSchema,
} from "../schemas/customers.schema";

describe("UpdateCustomerSchema", () => {
  it("rejects empty name", () => {
    const result = UpdateCustomerSchema.safeParse({ name: "" });
    expect(result.success).toBe(false);
  });

  it("accepts valid name", () => {
    const result = UpdateCustomerSchema.safeParse({ name: "Acme Corp" });
    expect(result.success).toBe(true);
  });

  it("accepts partial updates without name", () => {
    const result = UpdateCustomerSchema.safeParse({ street: "Main St" });
    expect(result.success).toBe(true);
  });

  it("rejects a name longer than 255 chars", () => {
    const result = UpdateCustomerSchema.safeParse({ name: "x".repeat(256) });
    expect(result.success).toBe(false);
  });

  it("accepts nullable address/identity fields set to null", () => {
    const result = UpdateCustomerSchema.safeParse({
      street: null,
      city: null,
      postal_code: null,
      country: null,
      company_id: null,
      vat_id: null,
    });
    expect(result.success).toBe(true);
  });
});

describe("CreateCustomerSchema", () => {
  it("requires a name", () => {
    const result = CreateCustomerSchema.safeParse({ street: "Main St" });
    expect(result.success).toBe(false);
  });

  it("accepts a full valid payload with all optional fields", () => {
    const result = CreateCustomerSchema.safeParse({
      name: "Acme Corp",
      street: "Main St 1",
      city: "Praha",
      postal_code: "11000",
      country: "CZ",
      company_id: "12345678",
      vat_id: "CZ12345678",
      custom_fields: [{ label: "Note", value: "VIP" }],
      customer_field_order: ["name", "street"],
    });
    expect(result.success).toBe(true);
  });

  it("accepts custom_fields as an array of arbitrary objects", () => {
    const result = CreateCustomerSchema.safeParse({
      name: "Acme Corp",
      custom_fields: [
        { label: "a", value: 1 },
        { nested: { deep: true } },
        "raw string",
      ],
    });
    expect(result.success).toBe(true);
  });

  it("rejects custom_fields that is not an array", () => {
    const result = CreateCustomerSchema.safeParse({
      name: "Acme Corp",
      custom_fields: { not: "an array" },
    });
    expect(result.success).toBe(false);
  });

  it("rejects custom_fields longer than the 100-item cap", () => {
    const result = CreateCustomerSchema.safeParse({
      name: "Acme Corp",
      custom_fields: Array.from({ length: 101 }, (_, i) => ({ i })),
    });
    expect(result.success).toBe(false);
  });

  it("rejects customer_field_order entries that are not strings", () => {
    const result = CreateCustomerSchema.safeParse({
      name: "Acme Corp",
      customer_field_order: ["name", 5],
    });
    expect(result.success).toBe(false);
  });

  it("rejects an over-length nullable field (vat_id > 255)", () => {
    const result = CreateCustomerSchema.safeParse({
      name: "Acme Corp",
      vat_id: "x".repeat(256),
    });
    expect(result.success).toBe(false);
  });
});
