from promptflow import PFClient
from typing import List, Dict
import requests


package_name = "oeahr_package"


def list_deployment_names(subscription_id, resource_group_name, workspace_name, connection_name) -> List[Dict[str, str]]:
    """This is an example to show how to get Azure ML resource in tool input list function.
    :param subscription_id: Azure subscription id.
    :param resource_group_name: Azure resource group name.
    :param workspace_name: Azure ML workspace name.
    :param prefix: prefix to add to each item.
    """
    from azure.identity import DefaultAzureCredential
    from azure.ai.ml import MLClient

    credential = DefaultAzureCredential()
    token = credential.get_token("https://management.azure.com/.default")
    headers = {
        "Authorization": f"Bearer {token.token}"
    }

    ml_client = MLClient(credential=credential, subscription_id=subscription_id, resource_group_name=resource_group_name)
    workspace = ml_client.workspaces.get(workspace_name)
    region = workspace.location

    url = f"https://ml.azure.com/api/{region}/flow/api/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.MachineLearningServices/workspaces/{workspace_name}/Connections/{connection_name}/AzureOpenAIDeployments"

    response = requests.get(url, headers=headers)
    response_json = response.json()

    output_dict = []

    for item in response_json:
        output_dict.append({"value": item["name"]})

    return output_dict


def list_package_tools(raise_error=False):
    """
    List the meta of all tools in the package.
    The key of meta dict is the module name of tools and value is the meta data of the tool.
    """
    # This function is auto generated by pf CLI, please do not modify manually.
    tools = {}
    pf_client = PFClient()
    tools = pf_client._tools._list_tools_in_package(package_name, raise_error=raise_error)
    return tools
