from typing import Any, cast

import requests


def generate_server_enrollment_token(
    team_name: str,
    project_name: str,
    org_name: str,
    bearer_token: str,
    resource_group_id: str,
    project_id: str,
    description: str = "Generated by script",
) -> dict[str, Any]:
    """
    Generate an enrollment token from Okta API.

    Args:
        team_name: The team name
        project_name: The project name
        org_name: The organization name
        bearer_token: The bearer token for authentication
        key_secret: The API key secret

    Returns:
        dict: The API response data
    """
    url = (
        f"https://{org_name}.pam.okta.com/v1/teams/{team_name}/resource_groups/"
        f"{resource_group_id}/projects/{project_id}/server_enrollment_tokens"
    )
    # POST endpoint for creating server enrollment tokens

    payload = {"description": description}

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {bearer_token}",
    }

    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()  # Raise an exception for bad status codes

    return cast(dict[str, Any], response.json())
