import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  PartnerNotFoundError,
  PartnerNotOrganizationError,
  InvalidFirstNameError,
  InvalidLastNameError,
  MissingCommunicationChannelError,
  InvalidEmailFormatError,
} from "../lib/errors.generated";
import { baseActivePartner, baseIndividualPartner } from "../testing/fixtures";
import { run } from "./createContactPerson";

describe("createContactPerson", () => {
  it("creates contact person", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);
    spies.insert.mockReturnValue({
      id: "new-contact",
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "jane@example.com",
      phone: null,
      jobTitle: null,
      department: null,
      isPrimary: false,
      status: "ACTIVE",
    });

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "jane@example.com",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.contact.status).toBe("ACTIVE");
      expect(result.value.contact.isPrimary).toBe(false);
    }
  });

  it("returns error when partner not found", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

    const result = await run(db, {
      partnerId: "nonexistent",
      firstName: "Jane",
      lastName: "Doe",
      email: "jane@example.com",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(PartnerNotFoundError);
    }
  });

  it("returns error when partner is INDIVIDUAL", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseIndividualPartner);

    const result = await run(db, {
      partnerId: baseIndividualPartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "jane@example.com",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(PartnerNotOrganizationError);
    }
  });

  it("returns error when firstName is empty", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "",
      lastName: "Doe",
      email: "jane@example.com",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidFirstNameError);
    }
  });

  it("returns error when lastName is empty", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "",
      email: "jane@example.com",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidLastNameError);
    }
  });

  it("returns error when neither email nor phone provided", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(MissingCommunicationChannelError);
    }
  });

  it("returns error when whitespace-only channels provided as only communication", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "   ",
      phone: "   ",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(MissingCommunicationChannelError);
    }
  });

  it("returns error for invalid email format", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "invalid-email",
    });

    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidEmailFormatError);
    }
  });

  it("creates with ACTIVE status", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);
    spies.insert.mockReturnValue({
      id: "new-contact",
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "jane@example.com",
      phone: null,
      jobTitle: null,
      department: null,
      isPrimary: false,
      status: "ACTIVE",
    });

    const result = await run(db, {
      partnerId: baseActivePartner.id,
      firstName: "Jane",
      lastName: "Doe",
      email: "jane@example.com",
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.contact.status).toBe("ACTIVE");
    }
  });
});
