# Copyright (c) 2025, JINGROW
# For license information, please see license.txt

from __future__ import annotations

import jingrow
from jingrow.model.page import Page


class SiteUserSession(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		otp: DF.Data | None
		otp_generated_at: DF.Datetime | None
		session_id: DF.Data | None
		user: DF.Data | None
		verified: DF.Check
	# end: auto-generated types

	def send_otp(self):
		"""Send OTP to the user for site login."""

		from jcloud.utils.otp import generate_otp

		self.otp = generate_otp()
		self.session_id = jingrow.generate_hash()
		self.otp_generated_at = jingrow.utils.now_datetime()
		if jingrow.conf.developer_mode and jingrow.local.dev_server:
			self.otp = 111111
		self.save()

		if jingrow.conf.developer_mode:
			print(f"\nOTP for {self.user} for site login:")
			print()
			print(self.otp)
			print()
			return

		subject = f"{self.otp} - OTP for Jingrow Site Login"
		args = {}

		args.update(
			{
				"otp": self.otp,
				"image_path": "http://git.jingrow.com/jingrow/gameplan/assets/9355208/447035d0-0686-41d2-910a-a3d21928ab94",
			}
		)

		jingrow.sendmail(
			recipients=self.user,
			subject=subject,
			template="verify_account_for_site_login",
			args=args,
			now=True,
		)
