import sys

import click

import influxframework
from influxframework.commands import get_site, pass_context
from influxframework.exceptions import SiteNotSpecifiedError
from influxframework.utils import cint


@click.command("trigger-scheduler-event", help="Trigger a scheduler event")
@click.argument("event")
@pass_context
def trigger_scheduler_event(context, event):
	import influxframework.utils.scheduler

	exit_code = 0

	for site in context.sites:
		try:
			influxframework.init(site=site)
			influxframework.connect()
			try:
				influxframework.get_doc("Scheduled Job Type", {"method": event}).execute()
			except influxframework.DoesNotExistError:
				click.secho(f"Event {event} does not exist!", fg="red")
				exit_code = 1
		finally:
			influxframework.destroy()

	if not context.sites:
		raise SiteNotSpecifiedError

	sys.exit(exit_code)


@click.command("enable-scheduler")
@pass_context
def enable_scheduler(context):
	"Enable scheduler"
	import influxframework.utils.scheduler

	for site in context.sites:
		try:
			influxframework.init(site=site)
			influxframework.connect()
			influxframework.utils.scheduler.enable_scheduler()
			influxframework.db.commit()
			print("Enabled for", site)
		finally:
			influxframework.destroy()
	if not context.sites:
		raise SiteNotSpecifiedError


@click.command("disable-scheduler")
@pass_context
def disable_scheduler(context):
	"Disable scheduler"
	import influxframework.utils.scheduler

	for site in context.sites:
		try:
			influxframework.init(site=site)
			influxframework.connect()
			influxframework.utils.scheduler.disable_scheduler()
			influxframework.db.commit()
			print("Disabled for", site)
		finally:
			influxframework.destroy()
	if not context.sites:
		raise SiteNotSpecifiedError


@click.command("scheduler")
@click.option("--site", help="site name")
@click.argument("state", type=click.Choice(["pause", "resume", "disable", "enable"]))
@pass_context
def scheduler(context, state, site=None):
	import influxframework.utils.scheduler
	from influxframework.installer import update_site_config

	if not site:
		site = get_site(context)

	try:
		influxframework.init(site=site)

		if state == "pause":
			update_site_config("pause_scheduler", 1)
		elif state == "resume":
			update_site_config("pause_scheduler", 0)
		elif state == "disable":
			influxframework.connect()
			influxframework.utils.scheduler.disable_scheduler()
			influxframework.db.commit()
		elif state == "enable":
			influxframework.connect()
			influxframework.utils.scheduler.enable_scheduler()
			influxframework.db.commit()

		print(f"Scheduler {state}d for site {site}")

	finally:
		influxframework.destroy()


@click.command("set-maintenance-mode")
@click.option("--site", help="site name")
@click.argument("state", type=click.Choice(["on", "off"]))
@pass_context
def set_maintenance_mode(context, state, site=None):
	from influxframework.installer import update_site_config

	if not site:
		site = get_site(context)

	try:
		influxframework.init(site=site)
		update_site_config("maintenance_mode", 1 if (state == "on") else 0)

	finally:
		influxframework.destroy()


@click.command(
	"doctor"
)  # Passing context always gets a site and if there is no use site it breaks
@click.option("--site", help="site name")
@pass_context
def doctor(context, site=None):
	"Get diagnostic info about background workers"
	from influxframework.utils.doctor import doctor as _doctor

	if not site:
		site = get_site(context, raise_err=False)
	return _doctor(site=site)


@click.command("show-pending-jobs")
@click.option("--site", help="site name")
@pass_context
def show_pending_jobs(context, site=None):
	"Get diagnostic info about background jobs"
	from influxframework.utils.doctor import pending_jobs as _pending_jobs

	if not site:
		site = get_site(context)

	with influxframework.init_site(site):
		pending_jobs = _pending_jobs(site=site)

	return pending_jobs


@click.command("purge-jobs")
@click.option("--site", help="site name")
@click.option("--queue", default=None, help='one of "low", "default", "high')
@click.option(
	"--event",
	default=None,
	help='one of "all", "weekly", "monthly", "hourly", "daily", "weekly_long", "daily_long"',
)
def purge_jobs(site=None, queue=None, event=None):
	"Purge any pending periodic tasks, if event option is not given, it will purge everything for the site"
	from influxframework.utils.doctor import purge_pending_jobs

	influxframework.init(site or "")
	count = purge_pending_jobs(event=event, site=site, queue=queue)
	print(f"Purged {count} jobs")


@click.command("schedule")
def start_scheduler():
	from influxframework.utils.scheduler import start_scheduler

	start_scheduler()


@click.command("worker")
@click.option("--queue", type=str)
@click.option("--quiet", is_flag=True, default=False, help="Hide Log Outputs")
@click.option("-u", "--rq-username", default=None, help="Redis ACL user")
@click.option("-p", "--rq-password", default=None, help="Redis ACL user password")
def start_worker(queue, quiet=False, rq_username=None, rq_password=None):
	"""Site is used to find redis credentals."""
	from influxframework.utils.background_jobs import start_worker

	start_worker(queue, quiet=quiet, rq_username=rq_username, rq_password=rq_password)


@click.command("ready-for-migration")
@click.option("--site", help="site name")
@pass_context
def ready_for_migration(context, site=None):
	from influxframework.utils.doctor import get_pending_jobs

	if not site:
		site = get_site(context)

	try:
		influxframework.init(site=site)
		pending_jobs = get_pending_jobs(site=site)

		if pending_jobs:
			print(f"NOT READY for migration: site {site} has pending background jobs")
			sys.exit(1)

		else:
			print(f"READY for migration: site {site} does not have any background jobs")
			return 0

	finally:
		influxframework.destroy()


commands = [
	disable_scheduler,
	doctor,
	enable_scheduler,
	purge_jobs,
	ready_for_migration,
	scheduler,
	set_maintenance_mode,
	show_pending_jobs,
	start_scheduler,
	start_worker,
	trigger_scheduler_event,
]
