import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([]);

    const result = await mock.withTx((trx) => run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseRootNode]);

    const result = await mock.withTx((trx) => run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseRootNode]);

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

  it("updates node name", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedNode = { ...baseRootNode, name: "Consumer Electronics" };
    mock.enqueueResults([baseRootNode], [updatedNode]);

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

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.taxonomyNode.name).toBe("Consumer Electronics");
    }
    expect(mock.updates.length).toBeGreaterThan(0);
  });

  it("looks up node by code and updates name", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedNode = { ...baseRootNode, name: "Consumer Electronics" };
    mock.enqueueResults([baseRootNode], [updatedNode]);

    const result = await mock.withTx((trx) =>
      run(trx, { 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 mock = createKyselyMock<Namespace["main-db"]>();
    const updatedNode = { ...baseRootNode, sortOrder: 5 };
    mock.enqueueResults([baseRootNode], [updatedNode]);

    await mock.withTx((trx) =>
      run(trx, { id: baseRootNode.id, name: "Electronics", sortOrder: 5 }),
    );

    expect(mock.updates[0].updateValues()).toEqual(expect.objectContaining({ sortOrder: 5 }));
  });
});
