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

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

describe("activateAccount", () => {
  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 DRAFT", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseDraftAccount) // account lookup
      .mockReturnValueOnce(baseDraftCoa); // CoA lookup

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

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

  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(ChartOfAccountsNotActiveError);
    }
  });

  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("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("activates a DRAFT account", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const activated = { ...baseDraftAccount, status: "ACTIVE" };
    spies.select
      .mockReturnValueOnce(baseDraftAccount) // account lookup
      .mockReturnValueOnce(baseActiveCoa); // CoA lookup
    spies.update.mockReturnValue(activated);

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

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