import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [draftCompany], // company lookup
      [], // no departments
      [], // no sites
    );

    const result = await mock.withTx((trx) => run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) => run(trx, { companyId: "nonexistent" }));

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

  it("throws when company is ACTIVE", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([activeCompany]);

    const result = await mock.withTx((trx) => run(trx, { companyId: activeCompany.id }));

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

  it("throws when company is INACTIVE", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([inactiveCompany]);

    const result = await mock.withTx((trx) => run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [draftCompany], // company lookup
      [activeDepartment], // has departments
    );

    const result = await mock.withTx((trx) => run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [draftCompany], // company lookup
      [], // no departments
      [activeSite], // has sites
    );

    const result = await mock.withTx((trx) => run(trx, { companyId: draftCompany.id }));

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