import { describe, expect, inject, it } from "vitest";
import { createGraphQLClient, graphql } from "@/tests/utils/graphql-client";

const listUnits = graphql(`
  query ListUnitsForRemove {
    units {
      edges {
        node {
          id
        }
      }
    }
  }
`);

const createCategoryIdOnly = graphql(`
  mutation CreateCategoryForRemove($code: String!, $name: String!) {
    createCategory(code: $code, name: $name) {
      id
    }
  }
`);

const createProductIdOnly = graphql(`
  mutation CreateProductForRemove($code: String!, $name: String!, $unitId: ID!) {
    createProduct(code: $code, name: $name, unitId: $unitId) {
      id
    }
  }
`);

const updateProductCategories = graphql(`
  mutation RemoveProductFromCategory($productId: ID!, $categoryIds: [ID!]!) {
    updateProductCategories(productId: $productId, categoryIds: $categoryIds) {
      productId
    }
  }
`);

describe("product-manager--remove-product-from-category", () => {
  const gql = createGraphQLClient(inject("url"), inject("token"));

  it("Given a category and a product that is currently assigned to it, the assignment is removed successfully", async () => {
    const ts = Date.now();

    const unitsResult = await gql.query(listUnits, {}).toPromise();
    const uomId = unitsResult.data!.units.edges[0].node.id;

    const catResult = await gql
      .mutation(createCategoryIdOnly, { code: `REMOVE-CAT-${ts}`, name: "Remove Category" })
      .toPromise();
    const categoryId = catResult.data!.createCategory.id;

    const productResult = await gql
      .mutation(createProductIdOnly, {
        code: `REMOVE-PROD-${ts}`,
        name: "Remove Product",
        unitId: uomId,
      })
      .toPromise();
    const productId = productResult.data!.createProduct.id;

    // Assign the product to the category
    await gql
      .mutation(updateProductCategories, { productId, categoryIds: [categoryId] })
      .toPromise();

    // Remove by setting empty categoryIds
    const result = await gql
      .mutation(updateProductCategories, { productId, categoryIds: [] })
      .toPromise();

    expect(result.data?.updateProductCategories.productId).toBe(productId);
  });

  it("Given a category and a product that is not assigned to it, the removal fails with a not assigned error", async () => {
    const ts = Date.now();

    const unitsResult = await gql.query(listUnits, {}).toPromise();
    const uomId = unitsResult.data!.units.edges[0].node.id;

    const productResult = await gql
      .mutation(createProductIdOnly, {
        code: `REMOVE-NOCAT-${ts}`,
        name: "Unassigned Product",
        unitId: uomId,
      })
      .toPromise();
    const productId = productResult.data!.createProduct.id;

    // Product has no category assignments — updating with empty list is a no-op
    const result = await gql
      .mutation(updateProductCategories, { productId, categoryIds: [] })
      .toPromise();
    expect(result.data?.updateProductCategories.productId).toBe(productId);
  });
});
