import { describe, test, expect } from "bun:test";
import { QUOTE_STATUSES } from "../../src/types/QuoteStatuses";
import type { QuoteStatus } from "../../src/types/QuoteStatuses";

describe("QuoteStatuses", () => {
  test("QUOTE_STATUSES is a non-empty array", () => {
    expect(Array.isArray(QUOTE_STATUSES)).toBe(true);
    expect(QUOTE_STATUSES.length).toBeGreaterThan(0);
  });

  test("contains expected status names", () => {
    const names = QUOTE_STATUSES.map((s) => s.name);
    expect(names).toContain("New");
    expect(names).toContain("Won");
    expect(names).toContain("Lost");
    expect(names).toContain("Active");
    expect(names).toContain("Internal Review");
    expect(names).toContain("FutureLead");
  });

  test("each status has required fields", () => {
    for (const status of QUOTE_STATUSES) {
      expect(typeof status.id).toBe("number");
      expect(typeof status.name).toBe("string");
      expect(typeof status.wonFlag).toBe("boolean");
      expect(typeof status.lostFlag).toBe("boolean");
      expect(typeof status.closedFlag).toBe("boolean");
      expect(typeof status.inactiveFlag).toBe("boolean");
      expect(typeof status.defaultFlag).toBe("boolean");
      expect(typeof status.enteredBy).toBe("string");
      expect(typeof status.dateEntered).toBe("string");
      expect(status._info).toBeDefined();
      expect(typeof status._info.lastUpdated).toBe("string");
      expect(typeof status._info.updatedBy).toBe("string");
      expect(typeof status.connectWiseId).toBe("string");
      expect(Array.isArray(status.optimaEquivalency)).toBe(true);
    }
  });

  test("Won status has wonFlag true and closedFlag true", () => {
    const won = QUOTE_STATUSES.find((s) => s.name === "Won")!;
    expect(won.wonFlag).toBe(true);
    expect(won.closedFlag).toBe(true);
    expect(won.lostFlag).toBe(false);
  });

  test("Lost status has lostFlag true and closedFlag true", () => {
    const lost = QUOTE_STATUSES.find((s) => s.name === "Lost")!;
    expect(lost.lostFlag).toBe(true);
    expect(lost.closedFlag).toBe(true);
    expect(lost.wonFlag).toBe(false);
  });

  test("New status is the default", () => {
    const newStatus = QUOTE_STATUSES.find((s) => s.name === "New")!;
    expect(newStatus.defaultFlag).toBe(true);
  });

  test("Active status is open (not closed)", () => {
    const active = QUOTE_STATUSES.find((s) => s.name === "Active")!;
    expect(active.closedFlag).toBe(false);
    expect(active.wonFlag).toBe(false);
    expect(active.lostFlag).toBe(false);
  });

  test("each status has unique id", () => {
    const ids = QUOTE_STATUSES.map((s) => s.id);
    expect(new Set(ids).size).toBe(ids.length);
  });

  test("each status has an optimaEquivalency array", () => {
    for (const status of QUOTE_STATUSES) {
      expect(Array.isArray(status.optimaEquivalency)).toBe(true);
    }
  });

  test("only one status has defaultFlag true", () => {
    const defaults = QUOTE_STATUSES.filter((s) => s.defaultFlag);
    expect(defaults).toHaveLength(1);
  });
});
