import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { AttributeNotFoundError, MissingRequiredFieldError } from "../lib/errors.generated";
import { baseColorAttribute } from "../testing/fixtures";
import { run } from "./updateProductAttribute";

describe("updateProductAttribute", () => {
  it("returns error when attribute not found", 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(AttributeNotFoundError);
    }
  });

  it("returns error when no fields provided", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    mock.enqueueResults([baseColorAttribute]);

    const result = await mock.withTx((trx) => run(trx, { id: baseColorAttribute.id }));

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

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

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

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

  it("passes custom fields through to update", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedAttribute = { ...baseColorAttribute, name: "Updated Color", priority: 10 };

    mock.enqueueResults([baseColorAttribute], [updatedAttribute]);

    await mock.withTx((trx) =>
      run(trx, {
        id: baseColorAttribute.id,
        name: "Updated Color",
        priority: 10,
      }),
    );

    expect(mock.updates[0].updateValues()).toEqual(
      expect.objectContaining({
        priority: 10,
        name: "Updated Color",
      }),
    );
  });

  it("updates name successfully", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedAttribute = { ...baseColorAttribute, name: "Updated Color" };

    mock.enqueueResults([baseColorAttribute], [updatedAttribute]);

    const result = await mock.withTx((trx) =>
      run(trx, { id: baseColorAttribute.id, name: "Updated Color" }),
    );

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

  it("looks up attribute by code and updates name", async () => {
    const mock = createKyselyMock<Namespace["main-db"]>();
    const updatedAttribute = { ...baseColorAttribute, name: "Updated Color" };

    mock.enqueueResults([baseColorAttribute], [updatedAttribute]);

    const result = await mock.withTx((trx) =>
      run(trx, { code: baseColorAttribute.code, name: "Updated Color" }),
    );

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.attribute.name).toBe("Updated Color");
    }
  });
});
