# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

import base64
import datetime
import re
import time
from collections.abc import Callable
from typing import TYPE_CHECKING, Optional

import jingrow
from jingrow import _
from jingrow.model import log_types
from jingrow.monitor import get_trace_id
from jingrow.query_builder import PageType
from jingrow.utils import cint, cstr, now_datetime

if TYPE_CHECKING:
	from jingrow.model.page import Page
	from jingrow.model.meta import Meta


NAMING_SERIES_PATTERN = re.compile(r"^[\w\- \/.#{}]+$", re.UNICODE)
BRACED_PARAMS_PATTERN = re.compile(r"(\{[\w | #]+\})")


# Types that can be using in naming series fields
NAMING_SERIES_PART_TYPES = (
	int,
	str,
	datetime.datetime,
	datetime.date,
	datetime.time,
	datetime.timedelta,
)


class InvalidNamingSeriesError(jingrow.ValidationError):
	pass


class NamingSeries:
	__slots__ = ("series",)

	def __init__(self, series: str):
		self.series = series

		# Add default number part if missing
		if "#" not in self.series:
			self.series += ".#####"

	def validate(self):
		if "." not in self.series:
			jingrow.throw(
				_("Invalid naming series {}: dot (.) missing").format(jingrow.bold(self.series)),
				exc=InvalidNamingSeriesError,
			)

		if not NAMING_SERIES_PATTERN.match(self.series):
			jingrow.throw(
				_(
					"Special Characters except '-', '#', '.', '/', '{{' and '}}' not allowed in naming series {0}"
				).format(jingrow.bold(self.series)),
				exc=InvalidNamingSeriesError,
			)

	def generate_next_name(self, pg: "Page", *, ignore_validate=False) -> str:
		if not ignore_validate:
			self.validate()

		parts = self.series.split(".")
		return parse_naming_series(parts, pg=pg)

	def get_prefix(self) -> str:
		"""Naming series stores prefix to maintain a counter in DB. This prefix can be used to update counter or validations.

		e.g. `SINV-.YY.-.####` has prefix of `SINV-22-` in database for year 2022.
		"""

		prefix = None

		def fake_counter_backend(partial_series, digits):
			nonlocal prefix
			prefix = partial_series
			return "#" * digits

		# This function evaluates all parts till we hit numerical parts and then
		# sends prefix + digits to DB to find next number.
		# Instead of reimplementing the whole parsing logic in multiple places we
		# can just ask this function to give us the prefix.
		parse_naming_series(self.series, number_generator=fake_counter_backend)

		if prefix is None:
			jingrow.throw(_("Invalid Naming Series: {}").format(self.series))

		return prefix

	def get_preview(self, pg=None) -> list[str]:
		"""Generate preview of naming series without using DB counters"""
		generated_names = []
		for count in range(1, 4):

			def fake_counter(_prefix, digits):
				# ignore B023: binding `count` is not necessary because
				# function is evaluated immediately and it can not be done
				# because of function signature requirement
				return str(count).zfill(digits)

			generated_names.append(parse_naming_series(self.series, pg=pg, number_generator=fake_counter))
		return generated_names

	def update_counter(self, new_count: int) -> None:
		"""Warning: Incorrectly updating series can result in unusable transactions"""
		Series = jingrow.qb.PageType("Series")
		prefix = self.get_prefix()

		# Initialize if not present in DB
		if jingrow.db.get_value("Series", prefix, "name", order_by="name") is None:
			jingrow.qb.into(Series).insert(prefix, 0).columns("name", "current").run()

		(jingrow.qb.update(Series).set(Series.current, cint(new_count)).where(Series.name == prefix)).run()

	def get_current_value(self) -> int:
		prefix = self.get_prefix()
		return cint(jingrow.db.get_value("Series", prefix, "current", order_by="name"))


def set_new_name(pg):
	"""
	Sets the `name` property for the document based on various rules.

	1. If amended pg, set suffix.
	2. If `autoname` method is declared, then call it.
	3. If `autoname` property is set in the PageType (`meta`), then build it using the `autoname` property.
	4. If no rule defined, use hash.

	:param pg: Page to be named.
	"""

	pg.run_method("before_naming")

	meta = jingrow.get_meta(pg.pagetype)
	autoname = meta.autoname or ""

	if autoname.lower() != "prompt" and not jingrow.flags.in_import:
		pg.name = None

	if is_autoincremented(pg.pagetype, meta):
		pg.name = jingrow.db.get_next_sequence_val(pg.pagetype)
		return

	if getattr(pg, "amended_from", None):
		_set_amended_name(pg)
		if pg.name:
			return

	elif getattr(pg.meta, "issingle", False):
		pg.name = pg.pagetype

	if not pg.name:
		set_naming_from_document_naming_rule(pg)

	if not pg.name:
		pg.run_method("autoname")

	if not pg.name and autoname:
		set_name_from_naming_options(autoname, pg)

	# at this point, we fall back to name generation with the hash option
	if not pg.name:
		pg.name = make_autoname("hash", pg.pagetype)

	pg.name = validate_name(pg.pagetype, pg.name)


def is_autoincremented(pagetype: str, meta: Optional["Meta"] = None) -> bool:
	"""Checks if the pagetype has autoincrement autoname set"""

	if not meta:
		meta = jingrow.get_meta(pagetype)

	if not getattr(meta, "issingle", False) and meta.autoname == "autoincrement":
		return True

	return False


def set_name_from_naming_options(autoname, pg):
	"""
	Get a name based on the autoname field option
	"""

	_autoname = autoname.lower()

	if _autoname.startswith("field:"):
		pg.name = _field_autoname(autoname, pg)

		# if the autoname option is 'field:' and no name was derived, we need to
		# notify
		if not pg.name:
			fieldname = autoname[6:]
			jingrow.throw(_("{0} is required").format(pg.meta.get_label(fieldname)))

	elif _autoname.startswith("naming_series:"):
		set_name_by_naming_series(pg)
	elif _autoname.startswith("prompt"):
		_prompt_autoname(autoname, pg)
	elif _autoname.startswith("format:"):
		pg.name = _format_autoname(autoname, pg)
	elif "#" in autoname:
		pg.name = make_autoname(autoname, pg=pg)


def set_naming_from_document_naming_rule(pg):
	"""
	Evaluate rules based on "Page Naming Series" pagetype
	"""
	from jingrow.model.base_page import PAGETYPES_FOR_PAGETYPE

	IGNORED_PAGETYPES = {*log_types, *PAGETYPES_FOR_PAGETYPE, "DefaultValue", "Patch Log"}

	if pg.pagetype in IGNORED_PAGETYPES:
		return

	document_naming_rules = jingrow.cache_manager.get_pagetype_map(
		"Page Naming Rule",
		pg.pagetype,
		filters={"document_type": pg.pagetype, "disabled": 0},
		order_by="priority desc",
	)

	for d in document_naming_rules:
		jingrow.get_cached_pg("Page Naming Rule", d.name).apply(pg)
		if pg.name:
			break


def set_name_by_naming_series(pg):
	"""Sets name by the `naming_series` property"""
	if not pg.naming_series:
		pg.naming_series = get_default_naming_series(pg.pagetype)

	if not pg.naming_series:
		jingrow.throw(jingrow._("Naming Series mandatory"))

	pg.name = make_autoname(pg.naming_series + ".#####", "", pg)


def make_autoname(key="", pagetype="", pg="", *, ignore_validate=False):
	"""
	     Creates an autoname from the given key:

	     **Autoname rules:**

	              * The key is separated by '.'
	              * '####' represents a series. The string before this part becomes the prefix:
	                     Example: ABC.#### creates a series ABC0001, ABC0002 etc
	              * 'MM' represents the current month
	              * 'YY' and 'YYYY' represent the current year


	*Example:*

	              * DE./.YY./.MM./.##### will create a series like
	                DE/09/01/00001 where 09 is the year, 01 is the month and 00001 is the series
	"""
	if key == "hash":
		return (_get_timestamp_prefix() + _generate_random_string(7))[:10]

	series = NamingSeries(key)
	return series.generate_next_name(pg, ignore_validate=ignore_validate)


def _get_timestamp_prefix():
	ts = int(time.time() * 10)  # time in deciseconds
	# we ~~don't need~~ can't get ordering over entire lifetime, so we wrap the time.
	ts = ts % (32**4)
	ts_part = base64.b32hexencode(ts.to_bytes(length=5, byteorder="big")).decode()[-3:].lower()

	# First character is from request/job specific UUID, all documents created in this "session" will
	# have same prefix. This avoids collision between parallel jobs with reasonable probabililistic
	# guarantees.
	request_part = (get_trace_id() or "")[-1:]

	return request_part + ts_part


def _generate_random_string(length=10):
	"""Better version of jingrow.generate_hash for naming.

	This uses entire base32 instead of base16 used by generate_hash. So it has twice as many
	characters and hence more likely to have shorter common prefixes. i.e. slighly faster comparisons and less conflicts.

	Why not base36?
	It's not in standard library else using all characters is probably better approach.
	Why not base64?
	MySQL is case-insensitive, we can't use both upper and lower case characters.
	"""
	from secrets import token_bytes as get_random_bytes

	return base64.b32hexencode(get_random_bytes(length)).decode()[:length].lower()


def parse_naming_series(
	parts: list[str] | str,
	pagetype=None,
	pg: Optional["Page"] = None,
	number_generator: Callable[[str, int], str] | None = None,
) -> str:
	"""Parse the naming series and get next name.

	args:
	        parts: naming series parts (split by `.`)
	        pg: document to use for series that have parts using fieldnames
	        number_generator: Use different counter backend other than `tabSeries`. Primarily used for testing.
	"""

	name = ""
	_sentinel = object()
	if isinstance(parts, str):
		parts = parts.split(".")

	if not number_generator:
		number_generator = getseries

	series_set = False
	today = now_datetime()
	for e in parts:
		if not e:
			continue

		part = ""
		if e.startswith("#"):
			if not series_set:
				digits = len(e)
				part = number_generator(name, digits)
				series_set = True
		elif e == "YY":
			part = today.strftime("%y")
		elif e == "MM":
			part = today.strftime("%m")
		elif e == "DD":
			part = today.strftime("%d")
		elif e == "YYYY":
			part = today.strftime("%Y")
		elif e == "WW":
			part = determine_consecutive_week_number(today)
		elif e == "timestamp":
			part = str(today)
		elif pg and (e.startswith("{") or pg.get(e, _sentinel) is not _sentinel):
			e = e.replace("{", "").replace("}", "")
			part = pg.get(e)
		elif method := has_custom_parser(e):
			part = jingrow.get_attr(method[0])(pg, e)
		else:
			part = e

		if isinstance(part, str):
			name += part
		elif isinstance(part, NAMING_SERIES_PART_TYPES):
			name += cstr(part).strip()

	return name


def has_custom_parser(e):
	"""Returns true if the naming series part has a custom parser"""
	return jingrow.get_hooks("naming_series_variables", {}).get(e)


def determine_consecutive_week_number(datetime):
	"""Determines the consecutive calendar week"""
	m = datetime.month
	# ISO 8601 calandar week
	w = datetime.strftime("%V")
	# Ensure consecutiveness for the first and last days of a year
	if m == 1 and int(w) >= 52:
		w = "00"
	elif m == 12 and int(w) <= 1:
		w = "53"
	return w


def getseries(key, digits):
	# series created ?
	# Using jingrow.qb as jingrow.get_values does not allow order_by=None
	series = PageType("Series")
	current = (jingrow.qb.from_(series).where(series.name == key).for_update().select("current")).run()

	if current and current[0][0] is not None:
		current = current[0][0]
		# yes, update it
		jingrow.db.sql("UPDATE `tabSeries` SET `current` = `current` + 1 WHERE `name`=%s", (key,))
		current = cint(current) + 1
	else:
		# no, create it
		jingrow.db.sql("INSERT INTO `tabSeries` (`name`, `current`) VALUES (%s, 1)", (key,))
		current = 1
	return ("%0" + str(digits) + "d") % current


def revert_series_if_last(key, name, pg=None):
	"""
	Reverts the series for particular naming series:
	* key is naming series		- SINV-.YYYY-.####
	* name is actual name		- SINV-2021-0001

	1. This function split the key into two parts prefix (SINV-YYYY) & hashes (####).
	2. Use prefix to get the current index of that naming series from Series table
	3. Then revert the current index.

	*For custom naming series:*
	1. hash can exist anywhere, if it exist in hashes then it take normal flow.
	2. If hash doesn't exit in hashes, we get the hash from prefix, then update name and prefix accordingly.

	*Example:*
	        1. key = SINV-.YYYY.-
	                * If key doesn't have hash it will add hash at the end
	                * prefix will be SINV-YYYY based on this will get current index from Series table.
	        2. key = SINV-.####.-2021
	                * now prefix = SINV-#### and hashes = 2021 (hash doesn't exist)
	                * will search hash in key then accordingly get prefix = SINV-
	        3. key = ####.-2021
	                * prefix = #### and hashes = 2021 (hash doesn't exist)
	                * will search hash in key then accordingly get prefix = ""
	"""
	if ".#" in key:
		prefix, hashes = key.rsplit(".", 1)
		if "#" not in hashes:
			# get the hash part from the key
			hash = re.search("#+", key)
			if not hash:
				return
			name = name.replace(hashes, "")
			prefix = prefix.replace(hash.group(), "")
	else:
		prefix = key

	if "." in prefix:
		prefix = parse_naming_series(prefix.split("."), pg=pg)

	count = cint(name.replace(prefix, ""))
	series = PageType("Series")
	current = (jingrow.qb.from_(series).where(series.name == prefix).for_update().select("current")).run()

	if current and current[0][0] == count:
		jingrow.db.sql("UPDATE `tabSeries` SET `current` = `current` - 1 WHERE `name`=%s", prefix)


def get_default_naming_series(pagetype: str) -> str | None:
	"""get default value for `naming_series` property"""
	naming_series_options = jingrow.get_meta(pagetype).get_naming_series_options()

	# Return first truthy options
	# Empty strings are used to avoid populating forms by default
	for option in naming_series_options:
		if option:
			return option


def validate_name(pagetype: str, name: int | str):
	if not name:
		jingrow.throw(_("No Name Specified for {0}").format(pagetype))

	if isinstance(name, int):
		if is_autoincremented(pagetype):
			# this will set the sequence value to be the provided name/value and set it to be used
			# so that the sequence will start from the next value
			jingrow.db.set_next_sequence_val(pagetype, name, is_val_used=True)
			return name

		jingrow.throw(_("Invalid name type (integer) for varchar name column"), jingrow.NameError)

	if name.startswith("New " + pagetype):
		jingrow.throw(
			_("There were some errors setting the name, please contact the administrator"), jingrow.NameError
		)
	name = name.strip()

	if not jingrow.get_meta(pagetype).get("issingle") and (pagetype == name) and (name != "PageType"):
		jingrow.throw(_("Name of {0} cannot be {1}").format(pagetype, name), jingrow.NameError)

	special_characters = "<>"
	if re.findall(f"[{special_characters}]+", name):
		message = ", ".join(f"'{c}'" for c in special_characters)
		jingrow.throw(_("Name cannot contain special characters like {0}").format(message), jingrow.NameError)

	return name


def append_number_if_name_exists(pagetype, value, fieldname="name", separator="-", filters=None):
	if not filters:
		filters = dict()
	filters.update({fieldname: value})
	exists = jingrow.db.exists(pagetype, filters)

	regex = f"^{re.escape(value)}{separator}\\d+$"

	if exists:
		last = jingrow.db.sql(
			f"""SELECT `{fieldname}` FROM `tab{pagetype}`
			WHERE `{fieldname}` {jingrow.db.REGEX_CHARACTER} %s
			ORDER BY length({fieldname}) DESC,
			`{fieldname}` DESC LIMIT 1""",
			regex,
		)

		if last:
			count = str(cint(last[0][0].rsplit(separator, 1)[1]) + 1)
		else:
			count = "1"

		value = f"{value}{separator}{count}"

	return value


def _set_amended_name(pg):
	amend_naming_rule = jingrow.db.get_value(
		"Amended Page Naming Settings", {"document_type": pg.pagetype}, "action", cache=True
	)
	if not amend_naming_rule:
		amend_naming_rule = jingrow.db.get_single_value(
			"Page Naming Settings", "default_amend_naming", cache=True
		)

	if amend_naming_rule == "Default Naming":
		return

	am_id = 1
	am_prefix = pg.amended_from
	if jingrow.db.get_value(pg.pagetype, pg.amended_from, "amended_from"):
		am_id = cint(pg.amended_from.split("-")[-1]) + 1
		am_prefix = "-".join(pg.amended_from.split("-")[:-1])  # except the last hyphen

	pg.name = am_prefix + "-" + str(am_id)
	return pg.name


def _field_autoname(autoname, pg, skip_slicing=None):
	"""
	Generate a name using `PageType` field. This is called when the pagetype's
	`autoname` field starts with 'field:'
	"""
	fieldname = autoname if skip_slicing else autoname[6:]
	return (cstr(pg.get(fieldname)) or "").strip()


def _prompt_autoname(autoname, pg):
	"""
	Generate a name using Prompt option. This simply means the user will have to set the name manually.
	This is called when the pagetype's `autoname` field starts with 'prompt'.
	"""
	# set from __newname in save.py
	if not pg.name:
		jingrow.throw(_("Please set the document name"))


def _format_autoname(autoname: str, pg):
	"""
	Generate autoname by replacing all instances of braced params (fields, date params ('DD', 'MM', 'YY'), series)
	Independent of remaining string or separators.

	Example pattern: 'format:LOG-{MM}-{fieldname1}-{fieldname2}-{#####}'
	"""

	first_colon_index = autoname.find(":")
	autoname_value = autoname[first_colon_index + 1 :]

	def get_param_value_for_match(match):
		param = match.group()
		return parse_naming_series([param[1:-1]], pg=pg)

	# Replace braced params with their parsed value
	name = BRACED_PARAMS_PATTERN.sub(get_param_value_for_match, autoname_value)

	return name
