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

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

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

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

    const result = await mock.withTx((trx) =>
      run(trx, {
        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 mock = createKyselyMock<Namespace["main-db"]>();
    const existingAssignment = {
      ...baseAttributeAssignment,
      productId: baseDraftProduct.id,
      valueId: baseColorRed.id,
    };
    mock.enqueueResults([baseDraftProduct], [existingAssignment], []);

    const result = await mock.withTx((trx) =>
      run(trx, {
        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(mock.deletes.length).toBeGreaterThan(0);
  });
});
