import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import { Transaction } 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 { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined);

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

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

    const result = await run(db, { 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 { db, spies } = createMockDb<Transaction>();
    const updatedAttribute = { ...baseColorAttribute, name: "Updated Color", priority: 10 };

    spies.select.mockReturnValueOnce(baseColorAttribute);
    spies.update.mockReturnValue(updatedAttribute);

    await run(db, {
      id: baseColorAttribute.id,
      name: "Updated Color",
      priority: 10,
    });

    expect(spies.set).toHaveBeenCalledWith(
      expect.objectContaining({
        priority: 10,
        name: "Updated Color",
      }),
    );
  });

  it("updates name successfully", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedAttribute = { ...baseColorAttribute, name: "Updated Color" };

    spies.select.mockReturnValueOnce(baseColorAttribute);
    spies.update.mockReturnValue(updatedAttribute);

    const result = await run(db, { id: baseColorAttribute.id, name: "Updated Color" });

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

  it("looks up attribute by code and updates name", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const updatedAttribute = { ...baseColorAttribute, name: "Updated Color" };

    spies.select.mockReturnValueOnce(baseColorAttribute);
    spies.update.mockReturnValue(updatedAttribute);

    const result = await run(db, { code: baseColorAttribute.code, name: "Updated Color" });

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