import json

from actionstreamer.Config import WebServiceConfig
from actionstreamer.CommonFunctions import send_signed_request, get_exception_info
from actionstreamer.Model import SrtPublishCredential
from actionstreamer.Result import SrtPublishCredentialResult


def create_srt_publish_credential(ws_config: WebServiceConfig, media_path: str) -> SrtPublishCredentialResult:

    srt_publish_credential_result = SrtPublishCredentialResult()

    try:
        method: str = "POST"
        path: str = "v1/srt/publish-credential"
        url: str = ws_config.base_url + path

        headers: dict[str, str] = {}
        headers["Content-Type"] = "application/json"

        parameters: dict[str, str] = {}

        request_body: dict[str, str] = {}
        request_body["streamPath"] = media_path

        body: str = json.dumps(request_body)

        result = send_signed_request(ws_config, method, url, path, headers, parameters, body)

        if result.code == 200:
            json_dict = json.loads(result.description)
            srt_publish_credential_result.srt_publish_credential = SrtPublishCredential(**json_dict)

        srt_publish_credential_result.code = result.code
        srt_publish_credential_result.description = result.description

    except Exception as ex:
        filename, line_number = get_exception_info()

        if filename is not None and line_number is not None:
            print(f"Exception occurred at line {line_number} in {filename}")

        print(ex)

        srt_publish_credential_result.code = -1
        srt_publish_credential_result.description = "Exception in create_srt_publish_credential. Line number " + str(line_number)

    return srt_publish_credential_result