import { describe, expect, it } from "vitest";
import { createMockDb } from "../../../testing/index";
import type { Transaction } from "../generated/kysely-tailordb";
import {
  ProductNotFoundError,
  ProductArchivedError,
  AxisStructureLockedError,
  AssignmentNotFoundError,
} from "../lib/errors.generated";
import {
  baseDraftProduct,
  baseActiveProduct,
  baseArchivedProduct,
  baseColorAttribute,
  baseColorRed,
  baseAttributeAssignment,
} from "../testing/fixtures";
import { run } from "./removeAttributeValueFromProduct";

describe("removeAttributeValueFromProduct", () => {
  it("returns error when product not found", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(undefined); // product not found

    const result = await run(db, {
      productId: "nonexistent",
      attributeId: "attr-1",
      valueId: "value-1",
    });

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

  it("returns error when product is ARCHIVED", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseArchivedProduct); // product found (ARCHIVED)

    const result = await run(db, {
      productId: baseArchivedProduct.id,
      attributeId: baseColorAttribute.id,
      valueId: baseColorRed.id,
    });

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

  it("returns error when product is ACTIVE (axis structure locked)", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select.mockReturnValueOnce(baseActiveProduct); // product found (ACTIVE)

    const result = await run(db, {
      productId: baseActiveProduct.id,
      attributeId: baseColorAttribute.id,
      valueId: baseColorRed.id,
    });

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

  it("returns error when assignment not found", async () => {
    const { db, spies } = createMockDb<Transaction>();
    spies.select
      .mockReturnValueOnce(baseDraftProduct) // product found (DRAFT)
      .mockReturnValueOnce(undefined); // assignment not found

    const result = await run(db, {
      productId: baseDraftProduct.id,
      attributeId: baseColorAttribute.id,
      valueId: "nonexistent-value",
    });

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

  it("removes assignment successfully on DRAFT product", async () => {
    const { db, spies } = createMockDb<Transaction>();
    const existingAssignment = {
      ...baseAttributeAssignment,
      productId: baseDraftProduct.id,
      valueId: baseColorRed.id,
    };
    spies.select
      .mockReturnValueOnce(baseDraftProduct) // product found (DRAFT)
      .mockReturnValueOnce(existingAssignment); // assignment found
    spies.delete.mockReturnValueOnce(undefined);

    const result = await run(db, {
      productId: baseDraftProduct.id,
      attributeId: baseColorAttribute.id,
      valueId: baseColorRed.id,
    });

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.id).toBe(existingAssignment.id);
    }
    expect(spies.delete).toHaveBeenCalled();
  });
});
