import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import { Transaction } from "../generated/kysely-tailordb";
import { MissingRequiredFieldsError, NodeNotFoundError } from "../lib/errors.generated";
import { baseRootNode } from "../testing/fixtures";
import { run } from "./updateTaxonomyNode";

describe("updateTaxonomyNode", () => {
  it("returns error when node does not exist", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValue(undefined);

    const result = await run(db, { id: "nonexistent", name: "New Name" });
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(NodeNotFoundError);
    }
  });

  it("returns error when name is empty", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseRootNode);

    const result = await run(db, { id: baseRootNode.id, name: "" });
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(MissingRequiredFieldsError);
    }
  });

  it("returns error when name is not provided", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseRootNode);

    const result = await run(db, { id: baseRootNode.id });
    expect(result.ok).toBe(false);
    if (!result.ok) {
      expect(result.error).toBeInstanceOf(MissingRequiredFieldsError);
    }
  });

  it("updates node name", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedNode = { ...baseRootNode, name: "Consumer Electronics" };
    spies.select.mockReturnValueOnce(baseRootNode);
    spies.update.mockReturnValue(updatedNode);

    const result = await run(db, { id: baseRootNode.id, name: "Consumer Electronics" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.taxonomyNode.name).toBe("Consumer Electronics");
    }
    expect(spies.update).toHaveBeenCalled();
  });

  it("looks up node by code and updates name", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedNode = { ...baseRootNode, name: "Consumer Electronics" };
    spies.select.mockReturnValueOnce(baseRootNode);
    spies.update.mockReturnValue(updatedNode);

    const result = await run(db, { code: baseRootNode.code, name: "Consumer Electronics" });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.taxonomyNode.name).toBe("Consumer Electronics");
    }
  });

  it("passes custom fields through to set", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedNode = { ...baseRootNode, sortOrder: 5 };
    spies.select.mockReturnValueOnce(baseRootNode);
    spies.update.mockReturnValue(updatedNode);

    await run(db, { id: baseRootNode.id, name: "Electronics", sortOrder: 5 });

    expect(spies.set).toHaveBeenNthCalledWith(1, expect.objectContaining({ sortOrder: 5 }));
  });
});
