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

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

  it("updates partner name", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedPartner = { ...baseActivePartner, name: "Updated Name" };
    spies.select.mockReturnValueOnce(baseActivePartner);
    spies.update.mockReturnValue(updatedPartner);

    const result = await run(db, { 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(spies.update).toHaveBeenCalled();
  });

  it("updates preferred currency", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const currency = { id: "currency-eur", code: "EUR" };
    const updatedPartner = { ...baseActivePartner, preferredCurrencyId: "currency-eur" };
    spies.select
      .mockReturnValueOnce(baseActivePartner) // Partner lookup
      .mockReturnValueOnce(currency); // Currency lookup
    spies.update.mockReturnValue(updatedPartner);

    const result = await run(
      db,
      { 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 { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActivePartner);

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseActivePartner) // Partner lookup
      .mockReturnValueOnce(undefined); // Currency not found

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

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