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 { IdentificationNotFoundError } from "../lib/errors.generated";
import { baseTaxIdIdentification } from "../testing/fixtures";
import { run, type DeletePartnerIdentificationInput } from "./deletePartnerIdentification";

describe("deletePartnerIdentification", () => {
  const ctx: CommandContext = {
    actorId: "test-actor",
    permissions: ["business-partner:partner:deletePartnerIdentification"],
  };

  it("deletes an existing identification", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    // Select: identification found
    mock.enqueueResults([baseTaxIdIdentification]);

    const input: DeletePartnerIdentificationInput = {
      id: baseTaxIdIdentification.id,
    };

    const result = await mock.withTx((trx) => run(trx, input, ctx));
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.id).toBe(baseTaxIdIdentification.id);
    }
  });

  it("returns error when identification not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const input: DeletePartnerIdentificationInput = {
      id: "non-existent",
    };

    const result = await mock.withTx((trx) => run(trx, input, ctx));
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(IdentificationNotFoundError);
    }
  });
});
