import os

import jingrow
from jingrow.database.db_manager import DbManager
from jingrow.utils import cint


def setup_database():
	root_conn = get_root_connection(jingrow.flags.root_login, jingrow.flags.root_password)
	root_conn.commit()
	root_conn.sql("end")
	root_conn.sql(f'DROP DATABASE IF EXISTS "{jingrow.conf.db_name}"')

	# If user exists, just update password
	if root_conn.sql(f"SELECT 1 FROM pg_roles WHERE rolname='{jingrow.conf.db_name}'"):
		root_conn.sql(f"ALTER USER \"{jingrow.conf.db_name}\" WITH PASSWORD '{jingrow.conf.db_password}'")
	else:
		root_conn.sql(f"CREATE USER \"{jingrow.conf.db_name}\" WITH PASSWORD '{jingrow.conf.db_password}'")
	root_conn.sql(f'CREATE DATABASE "{jingrow.conf.db_name}"')
	root_conn.sql(f'GRANT ALL PRIVILEGES ON DATABASE "{jingrow.conf.db_name}" TO "{jingrow.conf.db_name}"')
	if psql_version := root_conn.sql("SHOW server_version_num", as_dict=True):
		semver_version_num = psql_version[0].get("server_version_num") or "140000"
		if cint(semver_version_num) > 150000:
			root_conn.sql(f'ALTER DATABASE "{jingrow.conf.db_name}" OWNER TO "{jingrow.conf.db_name}"')
	root_conn.close()


def bootstrap_database(verbose, source_sql=None):
	jingrow.connect()
	import_db_from_sql(source_sql, verbose)
	jingrow.connect()

	if "tabDefaultValue" not in jingrow.db.get_tables():
		import sys

		from click import secho

		secho(
			"Table 'tabDefaultValue' missing in the restored site. "
			"This happens when the backup fails to restore. Please check that the file is valid\n"
			"Do go through the above output to check the exact error message from MariaDB",
			fg="red",
		)
		sys.exit(1)


def import_db_from_sql(source_sql=None, verbose=False):
	if verbose:
		print("Starting database import...")
	db_name = jingrow.conf.db_name
	if not source_sql:
		source_sql = os.path.join(os.path.dirname(__file__), "framework_postgres.sql")
	DbManager(jingrow.local.db).restore_database(
		verbose, db_name, source_sql, db_name, jingrow.conf.db_password
	)
	if verbose:
		print("Imported from database %s" % source_sql)


def get_root_connection(root_login=None, root_password=None):
	if not jingrow.local.flags.root_connection:
		if not root_login:
			root_login = jingrow.conf.get("root_login") or None

		if not root_login:
			root_login = input("Enter postgres super user: ")

		if not root_password:
			root_password = jingrow.conf.get("root_password") or None

		if not root_password:
			from getpass import getpass

			root_password = getpass("Postgres super user password: ")

		jingrow.local.flags.root_connection = jingrow.database.get_db(
			socket=jingrow.conf.db_socket,
			host=jingrow.conf.db_host,
			port=jingrow.conf.db_port,
			user=root_login,
			password=root_password,
			cur_db_name=root_login,
		)

	return jingrow.local.flags.root_connection


def drop_user_and_database(db_name, root_login, root_password):
	root_conn = get_root_connection(
		jingrow.flags.root_login or root_login, jingrow.flags.root_password or root_password
	)
	root_conn.commit()
	root_conn.sql(
		"SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = %s",
		(db_name,),
	)
	root_conn.sql("end")
	root_conn.sql(f"DROP DATABASE IF EXISTS {db_name}")
	root_conn.sql(f"DROP USER IF EXISTS {db_name}")
