import type { CommandContext } from "@tailor-platform/erp-kit/core";
import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import {
  AccountNotFoundError,
  DuplicateAccountCodeError,
  InvalidAccountCodeError,
  NameRequiredError,
} from "../lib/errors.generated";
import { baseActiveAccount } from "../testing/fixtures";
import { run } from "./updateAccount";

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

describe("updateAccount", () => {
  it("returns error when account does not exist", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) => run(trx, { id: "missing" }, ctx));

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

  it("returns error when name is empty", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: baseActiveAccount.id, name: "" }, ctx),
    );

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

  it("returns error when changed code is not numeric", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: baseActiveAccount.id, code: "ABC" }, ctx),
    );

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

  it("returns error when changed code duplicates another account", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const other = { ...baseActiveAccount, id: "other-account", code: "1300" };
    mock.enqueueResults([baseActiveAccount], [other]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: baseActiveAccount.id, code: other.code }, ctx),
    );

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

  it("updates name, code, and account type", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updated = {
      ...baseActiveAccount,
      name: "Updated",
      code: "2200",
      accountType: "LIABILITY" as const,
    };
    mock.enqueueResults([baseActiveAccount], [], [updated]);

    const result = await mock.withTx((trx) =>
      run(
        trx,
        { id: baseActiveAccount.id, name: "Updated", code: "2200", accountType: "LIABILITY" },
        ctx,
      ),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.account.name).toBe("Updated");
      expect(result.value.account.code).toBe("2200");
      expect(result.value.account.accountType).toBe("LIABILITY");
    }
  });

  it("returns existing account when no changes are provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseActiveAccount]);

    const result = await mock.withTx((trx) => run(trx, { id: baseActiveAccount.id }, ctx));

    expect(result.ok).toBe(true);
    if (result.ok) expect(result.value.account).toEqual(baseActiveAccount);
  });
});
