# Copyright (c) 2019, JINGROW
# License: MIT. See LICENSE

import datetime
import json
from urllib.parse import parse_qs

import jingrow
from jingrow.utils import get_request_session


def make_request(method, url, auth=None, headers=None, data=None, json=None, params=None):
	auth = auth or ""
	data = data or {}
	headers = headers or {}

	try:
		s = get_request_session()
		response = jingrow.flags.integration_request = s.request(
			method, url, data=data, auth=auth, headers=headers, json=json, params=params
		)
		response.raise_for_status()

		# Check whether the response has a content-type, before trying to check what it is
		if content_type := response.headers.get("content-type"):
			if content_type == "text/plain; charset=utf-8":
				return parse_qs(response.text)
			elif content_type.startswith("application/") and content_type.split(";")[0].endswith("json"):
				return response.json()
			elif response.text:
				return response.text
		return
	except Exception as exc:
		jingrow.log_error()
		raise exc


def make_get_request(url, **kwargs):
	return make_request("GET", url, **kwargs)


def make_post_request(url, **kwargs):
	return make_request("POST", url, **kwargs)


def make_put_request(url, **kwargs):
	return make_request("PUT", url, **kwargs)


def make_patch_request(url, **kwargs):
	return make_request("PATCH", url, **kwargs)


def make_delete_request(url, **kwargs):
	return make_request("DELETE", url, **kwargs)


def create_request_log(
	data,
	integration_type=None,
	service_name=None,
	name=None,
	error=None,
	request_headers=None,
	output=None,
	**kwargs,
):
	"""
	DEPRECATED: The parameter integration_type will be removed in the next major release.
	Use is_remote_request instead.
	"""
	if integration_type == "Remote":
		kwargs["is_remote_request"] = 1

	elif integration_type == "Subscription Notification":
		kwargs["request_description"] = integration_type

	reference_pagetype = reference_pagename = None
	if "reference_pagetype" not in kwargs:
		if isinstance(data, str):
			data = json.loads(data)

		reference_pagetype = data.get("reference_pagetype")
		reference_pagename = data.get("reference_pagename")

	integration_request = jingrow.get_pg(
		{
			"pagetype": "Integration Request",
			"integration_request_service": service_name,
			"request_headers": get_json(request_headers),
			"data": get_json(data),
			"output": get_json(output),
			"error": get_json(error),
			"reference_pagetype": reference_pagetype,
			"reference_pagename": reference_pagename,
			**kwargs,
		}
	)

	if name:
		integration_request.flags._name = name

	integration_request.insert(ignore_permissions=True)
	jingrow.db.commit()

	return integration_request


def get_json(obj):
	return obj if isinstance(obj, str) else jingrow.as_json(obj, indent=1)


def json_handler(obj):
	if isinstance(obj, datetime.date | datetime.timedelta | datetime.datetime):
		return str(obj)
