import requests
import json
import os
from jinja2 import Template
import pytest
import assertions

#HOST_URL = {{ host_url }}


def _trigger_requests(req_method, url, header, data, proxies=None):
    print("\n\nRegenerating traffic from CloudVector events....")
    return requests.request(method=req_method, url=url, proxies=proxies, headers=header, data=data, verify=False)

{% set api_name = api_name | replace("{","") | replace("}","") %}
{% for method, api_info in apis_metadata.items() %}
{% set params_covered = [] %}
{% for param in api_info.get("params",[]) %}
{%- if param not in ['host','access_token', 'url_prefix'] %}
@pytest.mark.usefixtures('{{ param }}')
@pytest.mark.parametrize('{{ param }}_arg', [(pytest.lazy_fixture('{{ param }}'))])
{%- set params_covered = params_covered.append(param+'_arg') -%}
{% endif -%}
{% endfor %}
def test_{{ method | lower }}_{{ api_name | lower }}({{ params_covered|join(", ") }}{% if params_covered %},{% endif %} host, access_token, url_prefix, update_headers):
    data = {}
    {% for param in api_info.get("params",[]) %}{% if (param not in ('host', 'access_token','url_prefix')) %}data["{{ param }}"] = {{ param }}_arg{% endif %}
    {% endfor %}
    req = {
             "data": data,
             "headers": {{ api_info.get("header") }}
          }
    req["headers"] = update_headers(headers=req["headers"], api="{{ api_info.get("url") }}")
    if "Authorization" in req["headers"] and req["headers"]["Authorization"] is None:
        req["headers"]["Authorization"] = os.environ.get("CVDAST_ACCESS_TOKEN")
    if access_token:
        req["headers"]["Authorization"] = access_token
    print("for debugging:"+str(req))
    url = Template(host+url_prefix+"{{ api_info.get("url") }}").render(**data)
    resp = _trigger_requests("{{ method }}", url,
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_{{ api_name | lower}}(req,resp)
{% endfor %}

{% for method, api_info in apis_metadata.items() %}
{% for validations in custom_validations %}
{% set param_name = validations[1].split("=")[0] %}
{% set params_list = api_info.get("params",[]) %}
{% set params_covered = [] %}
{% for param in api_info.get("params",[]) %}
{% if param not in ['host','access_token', 'url_prefix'] %}
@pytest.mark.usefixtures('{{ param }}')
@pytest.mark.parametrize('{{ param }}_arg', [(pytest.lazy_fixture('{{ param }}'))])
{% set params_covered = params_covered.append(param+'_arg') %}
{% endif %}
{% endfor %}
def test_{{ method | lower }}_{{ api_name | lower }}_for_{{ validations[0] | lower }}({{ params_covered|join(", ") }}, host, access_token, url_prefix, update_headers):
    data = {}
    {{ validations[1] }}
    {% for param in api_info.get("params",[]) %}{% if (param not in ('host', 'access_token','url_prefix')) %}data["{{ param }}"] = {{ param }}_arg{% endif %}
    {% endfor %}
    req = {
             "data": data,
             "headers": {{ api_info.get("header") }}
          }
    req["headers"] = update_headers(headers=req["headers"], api="{{ api_info.get("url") }}")
    if "Authorization" in req["headers"] and req["headers"]["Authorization"] is None:
        req["headers"]["Authorization"] = os.environ.get("CVDAST_ACCESS_TOKEN")
    if access_token:
        req["headers"]["Authorization"] = access_token
    print("for debugging:"+str(req))
    url = Template(host+url_prefix+"{{ api_info.get("url") }}").render(**data)
    resp = _trigger_requests("{{ method }}", url,
                      header=req["headers"],
                      data=json.dumps(data))
    print(resp.status_code)
    print(resp.text)
    assertions.assert_for_custom_response(req,resp,{{ validations[2] }})
{% endfor %}
{% endfor %}