import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  AccountGroupNotFoundError,
  ChartOfAccountsArchivedError,
  HasChildGroupsError,
  HasAssignedAccountsError,
} from "../lib/errors.generated";
import {
  baseActiveCoa,
  baseArchivedCoa,
  baseRootGroup,
  baseChildGroup,
  baseActiveAccount,
} from "../testing/fixtures";
import { run } from "./deleteAccountGroup";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

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

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

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

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

  it("returns error when parent CoA is ARCHIVED", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const groupInArchivedCoa = { ...baseRootGroup, chartOfAccountsId: baseArchivedCoa.id };
    spies.select
      .mockReturnValueOnce(groupInArchivedCoa) // Group lookup
      .mockReturnValueOnce(baseArchivedCoa); // CoA lookup

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

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

  it("returns error when group has child groups", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseRootGroup) // Group lookup
      .mockReturnValueOnce(baseActiveCoa) // CoA lookup
      .mockReturnValueOnce([baseChildGroup]); // Child groups exist

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

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

  it("returns error when group has assigned accounts", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseRootGroup) // Group lookup
      .mockReturnValueOnce(baseActiveCoa) // CoA lookup
      .mockReturnValueOnce([]) // No child groups
      .mockReturnValueOnce([baseActiveAccount]); // Assigned accounts exist

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

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

  it("deletes a leaf group with no assigned accounts", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseChildGroup) // Group lookup
      .mockReturnValueOnce(baseActiveCoa) // CoA lookup
      .mockReturnValueOnce([]) // No child groups
      .mockReturnValueOnce([]); // No assigned accounts
    spies.delete.mockReturnValue(undefined);

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

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