import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  CompanyNotFoundError,
  InvalidStateError,
  HasDepartmentsError,
  HasSitesError,
} from "../lib/errors.generated";
import {
  draftCompany,
  activeCompany,
  inactiveCompany,
  activeDepartment,
  activeSite,
} from "../testing/fixtures";
import { run } from "./deleteCompany";

describe("deleteCompany", () => {
  it("deletes DRAFT company", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(draftCompany) // company lookup
      .mockReturnValueOnce(undefined) // no departments
      .mockReturnValueOnce(undefined); // no sites

    const result = await run(db, { companyId: draftCompany.id });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.companyId).toBe(draftCompany.id);
    }
  });

  it("throws when company does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, { companyId: "nonexistent" });

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

  it("throws when company is ACTIVE", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValue(activeCompany);

    const result = await run(db, { companyId: activeCompany.id });

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

  it("throws when company is INACTIVE", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValue(inactiveCompany);

    const result = await run(db, { companyId: inactiveCompany.id });

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

  it("throws when company has existing departments", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(draftCompany) // company lookup
      .mockReturnValueOnce(activeDepartment); // has departments

    const result = await run(db, { companyId: draftCompany.id });

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

  it("throws when company has existing sites", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(draftCompany) // company lookup
      .mockReturnValueOnce(undefined) // no departments
      .mockReturnValueOnce(activeSite); // has sites

    const result = await run(db, { companyId: draftCompany.id });

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