import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import { ChartOfAccountsNotFoundError, InvalidStateTransitionError } from "../lib/errors.generated";
import { baseDraftCoa, baseActiveCoa, baseArchivedCoa } from "../testing/fixtures";
import { run } from "./deleteChartOfAccounts";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

const ctx: CommandContext = {
  actorId: "test-actor",
  permissions: ["coa-management:deleteChartOfAccounts"],
};

describe("deleteChartOfAccounts", () => {
  it("returns error when CoA does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

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

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

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

    const result = await run(db, { id: baseActiveCoa.id }, ctx);

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

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

    const result = await run(db, { id: baseArchivedCoa.id }, ctx);

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

  it("deletes a DRAFT CoA with no child records", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseDraftCoa).mockReturnValueOnce([]).mockReturnValueOnce([]);

    const result = await run(db, { id: baseDraftCoa.id }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.success).toBe(true);
    }
  });

  it("deletes a DRAFT CoA and cascade-deletes all child Accounts", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseDraftCoa)
      .mockReturnValueOnce([{ id: "a1" }])
      .mockReturnValueOnce([]);
    // delete spy is called 3 times: Accounts, AccountGroups, CoA

    const result = await run(db, { id: baseDraftCoa.id }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.success).toBe(true);
    }
    // Verify delete was called (for Account, AccountGroup, and ChartOfAccounts)
    expect(spies.delete).toHaveBeenCalled();
  });

  it("deletes a DRAFT CoA and cascade-deletes all child AccountGroups", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseDraftCoa)
      .mockReturnValueOnce([])
      .mockReturnValueOnce([{ id: "g1" }]);

    const result = await run(db, { id: baseDraftCoa.id }, ctx);

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.success).toBe(true);
    }
    // The delete spy should have been called 3 times:
    // once for Account, once for AccountGroup, once for ChartOfAccounts
    expect(spies.delete).toHaveBeenCalledTimes(3);
  });
});
