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

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

const getCategory = graphql(`
  query GetCategoryViewerDetail($id: ID!) {
    productCategory(id: $id) {
      id
      code
      name
    }
  }
`);

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

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

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

describe("viewer--view-category-detail", () => {
  const gql = createGraphQLClient(inject("url"), inject("token"));

  it("Given a valid category ID, the viewer sees the category code, name, parent, children, and assigned products", async () => {
    const code = `VIEWER-DETAIL-${Date.now()}`;
    const createResult = await gql
      .mutation(createCategoryIdOnly, { code, name: "Viewer Detail Category" })
      .toPromise();
    const categoryId = createResult.data!.createCategory.id;

    const result = await gql.query(getCategory, { id: categoryId }).toPromise();

    expect(result.data?.productCategory?.id).toBe(categoryId);
    expect(result.data?.productCategory?.code).toBe(code);
    expect(result.data?.productCategory?.name).toBe("Viewer Detail Category");
  });

  it("Given a category with no assigned products, the products section shows an empty state", async () => {
    const code = `VIEWER-EMPTY-${Date.now()}`;
    const createResult = await gql
      .mutation(createCategoryIdOnly, { code, name: "Viewer Empty Products" })
      .toPromise();
    const categoryId = createResult.data!.createCategory.id;

    const result = await gql.query(getCategory, { id: categoryId }).toPromise();

    expect(result.data?.productCategory?.id).toBe(categoryId);
  });

  it("Given a category with assigned products, all assigned products are listed without mutation controls", async () => {
    const ts = Date.now();
    const catResult = await gql
      .mutation(createCategoryIdOnly, {
        code: `VIEWER-PROD-${ts}`,
        name: "Viewer Category With Products",
      })
      .toPromise();
    const categoryId = catResult.data!.createCategory.id;

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

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

    await gql
      .mutation(updateProductCategories, { productId, categoryIds: [categoryId] })
      .toPromise();

    const result = await gql.query(getCategory, { id: categoryId }).toPromise();

    expect(result.data?.productCategory?.id).toBe(categoryId);
  });
});
