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

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

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

const activateProductIdOnly = graphql(`
  mutation ActivateProductIdOnly($id: ID!) {
    activateProduct(id: $id) {
      id
    }
  }
`);

const deactivateProductFull = graphql(`
  mutation DeactivateProductFull($id: ID!) {
    deactivateProduct(id: $id) {
      id
      status
    }
  }
`);

const deactivateProductIdOnly = graphql(`
  mutation DeactivateProductIdOnly($id: ID!) {
    deactivateProduct(id: $id) {
      id
    }
  }
`);

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

  it("Given an ACTIVE product, when deactivation is confirmed, then the product status becomes ARCHIVED.", async () => {
    const unitsResult = await gql.query(listUnits, {}).toPromise();
    const uomId = unitsResult.data!.units.edges[0].node.id;

    const createResult = await gql
      .mutation(createProductIdOnly, {
        code: `DEACT-${Date.now()}`,
        name: "Deactivate Test",
        unitId: uomId,
      })
      .toPromise();
    const productId = createResult.data!.createProduct.id;

    await gql.mutation(activateProductIdOnly, { id: productId }).toPromise();

    const res = await gql.mutation(deactivateProductFull, { id: productId }).toPromise();

    expect(res.data?.deactivateProduct.status).toBe("ARCHIVED");
  });

  it("Given a DRAFT product, when deactivation is attempted, then an error is shown and status remains unchanged.", async () => {
    const unitsResult = await gql.query(listUnits, {}).toPromise();
    const uomId = unitsResult.data!.units.edges[0].node.id;

    const createResult = await gql
      .mutation(createProductIdOnly, {
        code: `DEACT-DRAFT-${Date.now()}`,
        name: "Draft Deactivate Test",
        unitId: uomId,
      })
      .toPromise();
    const productId = createResult.data!.createProduct.id;

    await expect(
      gql.mutation(deactivateProductIdOnly, { id: productId }).toPromise(),
    ).rejects.toThrow();
  });

  it("Given an ARCHIVED product, when deactivation is attempted, then an error is shown and status remains unchanged.", async () => {
    const unitsResult = await gql.query(listUnits, {}).toPromise();
    const uomId = unitsResult.data!.units.edges[0].node.id;

    const createResult = await gql
      .mutation(createProductIdOnly, {
        code: `DEACT-ARCH-${Date.now()}`,
        name: "Archived Deactivate Test",
        unitId: uomId,
      })
      .toPromise();
    const productId = createResult.data!.createProduct.id;

    await gql.mutation(activateProductIdOnly, { id: productId }).toPromise();
    await gql.mutation(deactivateProductIdOnly, { id: productId }).toPromise();

    await expect(
      gql.mutation(deactivateProductIdOnly, { id: productId }).toPromise(),
    ).rejects.toThrow();
  });
});
