import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  AccountNotFoundError,
  ChartOfAccountsArchivedError,
  InvalidStateTransitionError,
  HasPostedTransactionsError,
} from "../lib/errors.generated";
import {
  baseDraftAccount,
  baseActiveAccount,
  baseInactiveAccount,
  baseActiveCoa,
  baseArchivedCoa,
} from "../testing/fixtures";
import { run } from "./deleteAccount";
import type { CommandContext } from "@tailor-platform/erp-kit/module";

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

describe("deleteAccount", () => {
  it("returns error when account 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(AccountNotFoundError);
    }
  });

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

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

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

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

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

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

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

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

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

  it("returns error when DRAFT account has posted transactions", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const draftWithPosted = { ...baseDraftAccount, hasPostedTransactions: true };
    spies.select
      .mockReturnValueOnce(draftWithPosted) // account lookup
      .mockReturnValueOnce(baseActiveCoa); // CoA lookup

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

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

  it("deletes a DRAFT account with no posted transactions", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseDraftAccount) // account lookup
      .mockReturnValueOnce(baseActiveCoa); // CoA lookup

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

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