import frappe
from frappe import _

from lms.lms.utils import (
	can_create_courses,
	get_evaluation_details,
	get_membership,
	has_course_moderator_role,
	is_certified,
	is_instructor,
	redirect_to_courses_list,
)


def get_context(context):
	context.no_cache = 1

	try:
		course_name = frappe.form_dict["course"]
	except KeyError:
		redirect_to_courses_list()

	if course_name == "new-course":
		if not can_create_courses():
			message = "You do not have permission to access this page."
			if frappe.session.user == "Guest":
				message = "Please login to access this page."

			raise frappe.PermissionError(_(message))

		context.course = frappe._dict()
		context.course.edit_mode = True
		context.membership = None
	else:
		set_course_context(context, course_name)


def set_course_context(context, course_name):
	course = frappe.db.get_value(
		"LMS Course",
		course_name,
		[
			"name",
			"title",
			"image",
			"short_introduction",
			"description",
			"published",
			"upcoming",
			"disable_self_learning",
			"status",
			"video_link",
			"enable_certification",
			"grant_certificate_after",
			"paid_certificate",
			"price_certificate",
			"currency",
			"max_attempts",
			"duration",
		],
		as_dict=True,
	)

	if frappe.form_dict.get("edit"):
		if not is_instructor(course.name) and not has_course_moderator_role():
			raise frappe.PermissionError(_("You do not have permission to access this page."))
		course.edit_mode = True

	if course is None:
		redirect_to_courses_list()

	related_courses = frappe.get_all(
		"Related Courses", {"parent": course.name}, ["course"]
	)
	for csr in related_courses:
		csr.update(
			frappe.db.get_value(
				"LMS Course",
				csr.course,
				["name", "upcoming", "title", "image", "enable_certification"],
				as_dict=True,
			)
		)
	course.related_courses = related_courses

	context.course = course
	membership = get_membership(course.name, frappe.session.user)
	context.course.query_parameter = (
		"?batch=" + membership.batch if membership and membership.batch else ""
	)
	context.membership = membership
	context.certificate = is_certified(course.name)
	eval_details = get_evaluation_details(course.name)
	context.eligible_for_evaluation = eval_details.eligible
	context.certificate_request = eval_details.request
	context.no_of_attempts = eval_details.no_of_attempts
	if context.course.upcoming:
		context.is_user_interested = get_user_interest(context.course.name)

	context.metatags = {
		"title": course.title,
		"image": course.image,
		"description": course.short_introduction,
		"keywords": course.title,
	}


def get_user_interest(course):
	return frappe.db.count(
		"LMS Course Interest", {"course": course, "user": frappe.session.user}
	)
