import type { CommandContext } from "@tailor-platform/erp-kit/core";
import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import { getCurrency } from "../../primitives/query/getCurrency.generated";
import type { Namespace } from "../generated/kysely-tailordb";
import {
  PartnerNotFoundError,
  InvalidNameError,
  CurrencyNotFoundError,
} from "../lib/errors.generated";
import { baseActivePartner } from "../testing/fixtures";
import { run } from "./updatePartner";

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

  it("updates partner name", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedPartner = { ...baseActivePartner, name: "Updated Name" };
    mock.enqueueResults([baseActivePartner], [updatedPartner]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.partner.name).toBe("Updated Name");
    }
    expect(mock.updates.length).toBeGreaterThan(0);
  });

  it("updates preferred currency", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const currency = { id: "currency-eur", code: "EUR" };
    const updatedPartner = { ...baseActivePartner, preferredCurrencyId: "currency-eur" };
    mock.enqueueResults(
      [baseActivePartner], // Partner lookup
      [currency], // Currency lookup
      [updatedPartner],
    );

    const result = await mock.withTx((trx) =>
      run(trx, { id: baseActivePartner.id, preferredCurrencyId: "currency-eur" }, ctx, {
        getCurrency,
      }),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.partner.preferredCurrencyId).toBe("currency-eur");
    }
  });

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

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

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

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

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

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

  it("returns error when currency not found", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults(
      [baseActivePartner], // Partner lookup
      [], // Currency not found
    );

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

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