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

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

describe("reactivateAccount", () => {
  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 = { ...baseInactiveAccount, 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 DRAFT", 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(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(InvalidStateTransitionError);
    }
  });

  it("returns error when account is already 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("reactivates an INACTIVE account", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const reactivated = { ...baseInactiveAccount, status: "ACTIVE", successorAccountId: null };
    spies.select
      .mockReturnValueOnce(baseInactiveAccount) // account lookup
      .mockReturnValueOnce(baseActiveCoa); // CoA lookup
    spies.update.mockReturnValue(reactivated);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.account.status).toBe("ACTIVE");
    }
  });

  it("clears successor mapping on reactivation", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const reactivated = { ...baseInactiveAccount, status: "ACTIVE", successorAccountId: null };
    spies.select
      .mockReturnValueOnce(baseInactiveAccount) // has successorAccountId
      .mockReturnValueOnce(baseActiveCoa);
    spies.update.mockReturnValue(reactivated);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.account.successorAccountId).toBeNull();
    }
  });
});
