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

const createRole = graphql(`
  mutation CreateRoleForToggle($name: String!, $permissions: [String!]!) {
    createRole(name: $name, permissions: $permissions) {
      id
    }
  }
`);

const createRoleIdOnly = graphql(`
  mutation CreateRoleIdOnlyForToggle($name: String!, $permissions: [String!]!) {
    createRole(name: $name, permissions: $permissions) {
      id
    }
  }
`);

const deactivateRole = graphql(`
  mutation DeactivateRole($roleId: ID!) {
    deactivateRole(roleId: $roleId) {
      id
      status
    }
  }
`);

const reactivateRole = graphql(`
  mutation ReactivateRole($roleId: ID!) {
    reactivateRole(roleId: $roleId) {
      id
      status
    }
  }
`);

const createUser = graphql(`
  mutation CreateUserForRoleToggle($name: String!, $email: String!, $roleIds: [ID!]) {
    createUser(name: $name, email: $email, roleIds: $roleIds) {
      id
    }
  }
`);

const deactivateRoleIdOnly = graphql(`
  mutation DeactivateRoleIdOnly($roleId: ID!) {
    deactivateRole(roleId: $roleId) {
      id
    }
  }
`);

describe("user--toggle-role-status", () => {
  const gql = createGraphQLClient(inject("url"), inject("token"));

  it("Open the detail page for an ACTIVE role with no user assignments and click Deactivate; verify the role's status changes to INACTIVE", async () => {
    const roleResult = await gql
      .mutation(createRole, {
        name: `DeactivateRole-${Date.now()}`,
        permissions: ["user-management:user:createUser"],
      })
      .toPromise();
    const res = await gql
      .mutation(deactivateRole, {
        roleId: roleResult.data!.createRole.id,
      })
      .toPromise();

    expect(res.data?.deactivateRole.status).toBe("INACTIVE");
  });

  it("Open the detail page for an INACTIVE role and click Reactivate; verify the role's status changes to ACTIVE", async () => {
    const roleResult = await gql
      .mutation(createRoleIdOnly, {
        name: `ReactivateRole-${Date.now()}`,
        permissions: ["user-management:user:createUser"],
      })
      .toPromise();

    await gql
      .mutation(deactivateRoleIdOnly, {
        roleId: roleResult.data!.createRole.id,
      })
      .toPromise();

    const res = await gql
      .mutation(reactivateRole, {
        roleId: roleResult.data!.createRole.id,
      })
      .toPromise();

    expect(res.data?.reactivateRole.status).toBe("ACTIVE");
  });

  it("Open the detail page for an ACTIVE role with active user assignments and click Deactivate; verify an error is shown and the role remains ACTIVE", async () => {
    const roleResult = await gql
      .mutation(createRoleIdOnly, {
        name: `AssignedRole-${Date.now()}`,
        permissions: ["user-management:user:createUser"],
      })
      .toPromise();
    const roleId = roleResult.data!.createRole.id;

    await gql
      .mutation(createUser, {
        name: "Role Toggle Test User",
        email: `role-toggle-${Date.now()}@example.com`,
        roleIds: [roleId],
      })
      .toPromise();

    await expect(gql.mutation(deactivateRoleIdOnly, { roleId }).toPromise()).rejects.toThrow();
  });
});
