import os

from . import __version__ as app_version

app_name = "jingrow"
app_title = "Jingrow"
app_publisher = "JINGROW"
app_description = "Full stack web framework with Python, Javascript, MariaDB, Redis, Node"
app_license = "MIT"
app_logo_url = "/assets/jingrow/images/jingrow-framework-logo.svg"
develop_version = "1.x.x-develop"

app_email = "developers@jingrow.com"

before_install = "jingrow.utils.install.before_install"
after_install = "jingrow.utils.install.after_install"

page_js = {"setup-wizard": "public/js/jingrow/setup_wizard.js"}

# website
app_include_js = [
	"libs.bundle.js",
	"desk.bundle.js",
	"list.bundle.js",
	"form.bundle.js",
	"controls.bundle.js",
	"report.bundle.js",
	"telemetry.bundle.js",
	"billing.bundle.js",
]

# 添加模板加载器 JS 文件 - 使用正确的资源路径格式
app_include_js.append("/assets/jingrow/js/jingrow/form/page_theme.js")
app_include_js.append("/assets/jingrow/js/jingrow.js")

app_include_css = [
	"desk.bundle.css",
	"report.bundle.css",
]
app_include_icons = [
	"jingrow/icons/timeless/icons.svg",
	"jingrow/icons/espresso/icons.svg",
]

pagetype_js = {
	"Web Page": "public/js/jingrow/utils/web_template.js",
	"Website Settings": "public/js/jingrow/utils/web_template.js",
}

web_include_js = ["website_script.js"]

web_include_css = []

email_css = ["email.bundle.css"]

website_route_rules = [
	{"from_route": "/blog/<category>", "to_route": "Blog Post"},
	{"from_route": "/kb/<category>", "to_route": "Help Article"},
	{"from_route": "/newsletters", "to_route": "Newsletter"},
	{"from_route": "/profile", "to_route": "me"},
	# Vue SPA routes (not under /app prefix)
	{"from_route": "/setup-wizard", "to_route": "app"},
	{"from_route": "/dashboard", "to_route": "app"},
	{"from_route": "/workspace/<path:workspace_path>", "to_route": "app"},
	{"from_route": "/flows", "to_route": "app"},
	{"from_route": "/flows/<path:flow_path>", "to_route": "app"},
	{"from_route": "/permission-manager", "to_route": "app"},
	{"from_route": "/tools", "to_route": "app"},
	{"from_route": "/tools/<path:tool_path>", "to_route": "app"},
	{"from_route": "/search", "to_route": "app"},
	{"from_route": "/dev/<path:dev_path>", "to_route": "app"},
	{"from_route": "/app-installer", "to_route": "app"},
	{"from_route": "/installed-apps", "to_route": "app"},
	{"from_route": "/app-marketplace", "to_route": "app"},
	{"from_route": "/app-marketplace/<path:marketplace_path>", "to_route": "app"},
	{"from_route": "/node-marketplace", "to_route": "app"},
	{"from_route": "/node-marketplace/<path:marketplace_path>", "to_route": "app"},
	{"from_route": "/agent-marketplace", "to_route": "app"},
	{"from_route": "/agent-marketplace/<path:marketplace_path>", "to_route": "app"},
	{"from_route": "/tool-marketplace", "to_route": "app"},
	{"from_route": "/tool-marketplace/<path:marketplace_path>", "to_route": "app"},
	{"from_route": "/publish-tool", "to_route": "app"},
	{"from_route": "/publish-app", "to_route": "app"},
	{"from_route": "/my-published-apps", "to_route": "app"},
	{"from_route": "/my-published-nodes", "to_route": "app"},
	{"from_route": "/my-published-agents", "to_route": "app"},
	{"from_route": "/my-published-tools", "to_route": "app"},
	{"from_route": "/page/<path:page_path>", "to_route": "app"},
	{"from_route": "/mindmap", "to_route": "app"},
	{"from_route": "/whiteboard", "to_route": "app"},
	# Original /app routes
	{"from_route": "/app/<path:app_path>", "to_route": "app"},
	{"from_route": "/login", "to_route": "app"},
	{"from_route": "/signup", "to_route": "app"},
]

website_redirects = [
	{"source": r"/desk(.*)", "target": r"/app\1"},
	{
		"source": "/.well-known/openid-configuration",
		"target": "/api/action/jingrow.integrations.oauth2.openid_configuration",
	},
]

base_template = "templates/base.html"

write_file_keys = ["file_url", "file_name"]

notification_config = "jingrow.core.notifications.get_notification_config"

before_tests = "jingrow.utils.install.before_tests"

email_append_to = ["Event", "ToDo", "Email"]

calendars = ["Event"]

leaderboards = "jingrow.desk.leaderboard.get_leaderboards"

# login

on_session_creation = [
	"jingrow.core.pagetype.system_log.feed.login_feed",
	"jingrow.core.pagetype.user.user.notify_admin_access_to_system_manager",
]

on_logout = "jingrow.core.pagetype.session_default_settings.session_default_settings.clear_session_defaults"

# PDF
pdf_header_html = "jingrow.utils.pdf.pdf_header_html"
pdf_body_html = "jingrow.utils.pdf.pdf_body_html"
pdf_footer_html = "jingrow.utils.pdf.pdf_footer_html"

# permissions

permission_query_conditions = {
	"Event": "jingrow.desk.pagetype.event.event.get_permission_query_conditions",
	"ToDo": "jingrow.desk.pagetype.todo.todo.get_permission_query_conditions",
	"User": "jingrow.core.pagetype.user.user.get_permission_query_conditions",
	"Dashboard Settings": "jingrow.desk.pagetype.dashboard_settings.dashboard_settings.get_permission_query_conditions",
	"Notification Log": "jingrow.desk.pagetype.notification_log.notification_log.get_permission_query_conditions",
	"Dashboard": "jingrow.desk.pagetype.dashboard.dashboard.get_permission_query_conditions",
	"Dashboard Chart": "jingrow.desk.pagetype.dashboard_chart.dashboard_chart.get_permission_query_conditions",
	"Number Card": "jingrow.desk.pagetype.number_card.number_card.get_permission_query_conditions",
	"Notification Settings": "jingrow.desk.pagetype.notification_settings.notification_settings.get_permission_query_conditions",
	"Note": "jingrow.desk.pagetype.note.note.get_permission_query_conditions",
	"Kanban Board": "jingrow.desk.pagetype.kanban_board.kanban_board.get_permission_query_conditions",
	"Contact": "jingrow.contacts.address_and_contact.get_permission_query_conditions_for_contact",
	"Address": "jingrow.contacts.address_and_contact.get_permission_query_conditions_for_address",
	"Email": "jingrow.core.pagetype.email.email.get_permission_query_conditions_for_email",
	"Workflow Action": "jingrow.workflow.pagetype.workflow_action.workflow_action.get_permission_query_conditions",
	"Prepared Report": "jingrow.core.pagetype.prepared_report.prepared_report.get_permission_query_condition",
	"File": "jingrow.core.pagetype.file.file.get_permission_query_conditions",
}

has_permission = {
	"Event": "jingrow.desk.pagetype.event.event.has_permission",
	"ToDo": "jingrow.desk.pagetype.todo.todo.has_permission",
	"Note": "jingrow.desk.pagetype.note.note.has_permission",
	"User": "jingrow.core.pagetype.user.user.has_permission",
	"Dashboard Chart": "jingrow.desk.pagetype.dashboard_chart.dashboard_chart.has_permission",
	"Number Card": "jingrow.desk.pagetype.number_card.number_card.has_permission",
	"Kanban Board": "jingrow.desk.pagetype.kanban_board.kanban_board.has_permission",
	"Contact": "jingrow.contacts.address_and_contact.has_permission",
	"Address": "jingrow.contacts.address_and_contact.has_permission",
	"Email": "jingrow.core.pagetype.email.email.has_permission",
	"Workflow Action": "jingrow.workflow.pagetype.workflow_action.workflow_action.has_permission",
	"File": "jingrow.core.pagetype.file.file.has_permission",
	"Prepared Report": "jingrow.core.pagetype.prepared_report.prepared_report.has_permission",
	"Notification Settings": "jingrow.desk.pagetype.notification_settings.notification_settings.has_permission",
}

has_website_permission = {"Address": "jingrow.contacts.pagetype.address.address.has_website_permission"}

jinja = {
	"methods": "jingrow.utils.jinja_globals",
	"filters": [
		"jingrow.utils.data.global_date_format",
		"jingrow.utils.markdown",
		"jingrow.website.utils.abs_url",
	],
}

standard_queries = {"User": "jingrow.core.pagetype.user.user.user_query"}

pg_events = {
	"*": {
		"on_update": [
			"jingrow.desk.notifications.clear_pagetype_notifications",
			"jingrow.workflow.pagetype.workflow_action.workflow_action.process_workflow_actions",
			"jingrow.core.pagetype.file.utils.attach_files_to_document",
			"jingrow.automation.pagetype.assignment_rule.assignment_rule.apply",
			"jingrow.automation.pagetype.assignment_rule.assignment_rule.update_due_date",
			"jingrow.core.pagetype.user_type.user_type.apply_permissions_for_non_standard_user_type",
			"jingrow.ai.utils.pg_status_handler.process_status_task",
		],
		"after_rename": "jingrow.desk.notifications.clear_pagetype_notifications",
		"on_cancel": [
			"jingrow.desk.notifications.clear_pagetype_notifications",
			"jingrow.workflow.pagetype.workflow_action.workflow_action.process_workflow_actions",
			"jingrow.automation.pagetype.assignment_rule.assignment_rule.apply",
		],
		"on_trash": [
			"jingrow.desk.notifications.clear_pagetype_notifications",
			"jingrow.workflow.pagetype.workflow_action.workflow_action.process_workflow_actions",
		],
		"on_update_after_submit": [
			"jingrow.workflow.pagetype.workflow_action.workflow_action.process_workflow_actions",
			"jingrow.automation.pagetype.assignment_rule.assignment_rule.apply",
			"jingrow.automation.pagetype.assignment_rule.assignment_rule.update_due_date",
			"jingrow.core.pagetype.file.utils.attach_files_to_document",
		],
		"on_change": [
			"jingrow.social.pagetype.energy_point_rule.energy_point_rule.process_energy_points",
			"jingrow.automation.pagetype.milestone_tracker.milestone_tracker.evaluate_milestone",
		],
	},
	"Event": {
		"after_insert": "jingrow.integrations.pagetype.google_calendar.google_calendar.insert_event_in_google_calendar",
		"on_update": "jingrow.integrations.pagetype.google_calendar.google_calendar.update_event_in_google_calendar",
		"on_trash": "jingrow.integrations.pagetype.google_calendar.google_calendar.delete_event_from_google_calendar",
	},
	"Contact": {
		"after_insert": "jingrow.integrations.pagetype.google_contacts.google_contacts.insert_contacts_to_google_contacts",
		"on_update": "jingrow.integrations.pagetype.google_contacts.google_contacts.update_contacts_to_google_contacts",
	},
	"PageType": {
		"on_update": "jingrow.cache_manager.build_domain_restriced_pagetype_cache",
	},
	"Page": {
		"on_update": "jingrow.cache_manager.build_domain_restriced_page_cache",
	},
}

scheduler_events = {
	"cron": {
		# 15 minutes
		"0/15 * * * *": [
			"jingrow.oauth.delete_oauth2_data",
			"jingrow.website.pagetype.web_page.web_page.check_publish_status",
			"jingrow.twofactor.delete_all_barcodes_for_users",
			"jingrow.email.pagetype.email_account.email_account.notify_unreplied",
			"jingrow.utils.global_search.sync_global_search",
			"jingrow.deferred_insert.save_to_db",
			"jingrow.automation.pagetype.reminder.reminder.send_reminders",
		],
		# 5 minutes
		"0/5 * * * *": [
			"jingrow.email.pagetype.email_account.email_account.pull",
		],
		# Hourly but offset by 30 minutes
		"30 * * * *": [
			"jingrow.core.pagetype.prepared_report.prepared_report.expire_stalled_report",
		],
		# Daily but offset by 45 minutes
		"45 0 * * *": [
			"jingrow.core.pagetype.log_settings.log_settings.run_log_clean_up",
		],
	},
	"all": [
		"jingrow.email.queue.flush",
		"jingrow.monitor.flush",
	],
	"hourly": [
		"jingrow.model.utils.link_count.update_link_count",
		"jingrow.model.utils.user_settings.sync_user_settings",
		"jingrow.desk.page.backups.backups.delete_downloadable_backups",
		"jingrow.desk.form.page_follow.send_hourly_updates",
		"jingrow.email.pagetype.newsletter.newsletter.send_scheduled_email",
		"jingrow.website.pagetype.personal_data_deletion_request.personal_data_deletion_request.process_data_deletion_request",
	],
	"daily": [
		"jingrow.desk.notifications.clear_notifications",
		"jingrow.desk.pagetype.event.event.send_event_digest",
		"jingrow.sessions.clear_expired_sessions",
		"jingrow.email.pagetype.notification.notification.trigger_daily_alerts",
		"jingrow.website.pagetype.personal_data_deletion_request.personal_data_deletion_request.remove_unverified_record",
		"jingrow.desk.form.page_follow.send_daily_updates",
		"jingrow.automation.pagetype.auto_repeat.auto_repeat.make_auto_repeat_entry",
	],
	"daily_long": [
		"jingrow.email.pagetype.auto_email_report.auto_email_report.send_daily",
	],
	"weekly_long": [
		"jingrow.desk.form.page_follow.send_weekly_updates",
		"jingrow.utils.change_log.check_for_update",
	],
	"monthly": [
		"jingrow.email.pagetype.auto_email_report.auto_email_report.send_monthly",
	],
	"monthly_long": [
	],
}

sounds = [
	{"name": "email", "src": "/assets/jingrow/sounds/email.mp3", "volume": 0.1},
	{"name": "submit", "src": "/assets/jingrow/sounds/submit.mp3", "volume": 0.1},
	{"name": "cancel", "src": "/assets/jingrow/sounds/cancel.mp3", "volume": 0.1},
	{"name": "delete", "src": "/assets/jingrow/sounds/delete.mp3", "volume": 0.05},
	{"name": "click", "src": "/assets/jingrow/sounds/click.mp3", "volume": 0.05},
	{"name": "error", "src": "/assets/jingrow/sounds/error.mp3", "volume": 0.1},
	{"name": "alert", "src": "/assets/jingrow/sounds/alert.mp3", "volume": 0.2},
	# {"name": "chime", "src": "/assets/jingrow/sounds/chime.mp3"},
]

setup_wizard_exception = [
	"jingrow.desk.page.setup_wizard.setup_wizard.email_setup_wizard_exception",
	"jingrow.desk.page.setup_wizard.setup_wizard.log_setup_wizard_exception",
]

before_migrate = ["jingrow.core.pagetype.patch_log.patch_log.before_migrate"]
after_migrate = ["jingrow.website.pagetype.website_theme.website_theme.after_migrate"]

otp_methods = ["OTP App", "Email", "SMS"]

user_data_fields = [
	{"pagetype": "Access Log", "strict": True},
	{"pagetype": "System Log", "strict": True},
	{"pagetype": "Comment", "strict": True},
	{
		"pagetype": "Contact",
		"filter_by": "email_id",
		"redact_fields": ["first_name", "last_name", "phone", "mobile_no"],
		"rename": True,
	},
	{"pagetype": "Contact Email", "filter_by": "email_id"},
	{
		"pagetype": "Address",
		"filter_by": "email_id",
		"redact_fields": [
			"address_title",
			"address_line1",
			"address_line2",
			"city",
			"county",
			"state",
			"pincode",
			"phone",
			"fax",
		],
	},
	{
		"pagetype": "Email",
		"filter_by": "sender",
		"redact_fields": ["sender_full_name", "phone_no", "content"],
	},
	{"pagetype": "Email", "filter_by": "recipients"},
	{"pagetype": "Email Group Member", "filter_by": "email"},
	{"pagetype": "Email Unsubscribe", "filter_by": "email", "partial": True},
	{"pagetype": "Email Queue", "filter_by": "sender"},
	{"pagetype": "Email Queue Recipient", "filter_by": "recipient"},
	{
		"pagetype": "File",
		"filter_by": "attached_to_name",
		"redact_fields": ["file_name", "file_url"],
	},
	{
		"pagetype": "User",
		"filter_by": "name",
		"redact_fields": [
			"email",
			"username",
			"first_name",
			"middle_name",
			"last_name",
			"full_name",
			"birth_date",
			"user_image",
			"phone",
			"mobile_no",
			"location",
			"banner_image",
			"interest",
			"bio",
			"email_signature",
		],
	},
	{"pagetype": "Version", "strict": True},
]

global_search_pagetypes = {
	"Default": [
		{"pagetype": "Contact"},
		{"pagetype": "Address"},
		{"pagetype": "ToDo"},
		{"pagetype": "Note"},
		{"pagetype": "Event"},
		{"pagetype": "Blog Post"},
		{"pagetype": "Dashboard"},
		{"pagetype": "Country"},
		{"pagetype": "Currency"},
		{"pagetype": "Newsletter"},
		{"pagetype": "Letter Head"},
		{"pagetype": "Workflow"},
		{"pagetype": "Web Page"},
		{"pagetype": "Web Form"},
	]
}

override_whitelisted_methods = {
	# Legacy File APIs
	"jingrow.utils.file_manager.download_file": "download_file",
	"jingrow.core.pagetype.file.file.download_file": "download_file",
	"jingrow.core.pagetype.file.file.unzip_file": "jingrow.core.api.file.unzip_file",
	"jingrow.core.pagetype.file.file.get_attached_images": "jingrow.core.api.file.get_attached_images",
	"jingrow.core.pagetype.file.file.get_files_in_folder": "jingrow.core.api.file.get_files_in_folder",
	"jingrow.core.pagetype.file.file.get_files_by_search_text": "jingrow.core.api.file.get_files_by_search_text",
	"jingrow.core.pagetype.file.file.get_max_file_size": "jingrow.core.api.file.get_max_file_size",
	"jingrow.core.pagetype.file.file.create_new_folder": "jingrow.core.api.file.create_new_folder",
	"jingrow.core.pagetype.file.file.move_file": "jingrow.core.api.file.move_file",
	"jingrow.core.pagetype.file.file.zip_files": "jingrow.core.api.file.zip_files",
	# Legacy (& Consistency) OAuth2 APIs
	"jingrow.www.login.login_via_google": "jingrow.integrations.oauth2_logins.login_via_google",
	"jingrow.www.login.login_via_github": "jingrow.integrations.oauth2_logins.login_via_github",
	"jingrow.www.login.login_via_facebook": "jingrow.integrations.oauth2_logins.login_via_facebook",
	"jingrow.www.login.login_via_jingrow": "jingrow.integrations.oauth2_logins.login_via_jingrow",
	"jingrow.www.login.login_via_office365": "jingrow.integrations.oauth2_logins.login_via_office365",
	"jingrow.www.login.login_via_salesforce": "jingrow.integrations.oauth2_logins.login_via_salesforce",
	"jingrow.www.login.login_via_fairlogin": "jingrow.integrations.oauth2_logins.login_via_fairlogin",
}

ignore_links_on_delete = [
	"Email",
	"ToDo",
	"PageShare",
	"Email Unsubscribe",
	"System Log",
	"File",
	"Version",
	"Page Follow",
	"Comment",
	"View Log",
	"Tag Link",
	"Notification Log",
	"Email Queue",
	"Page Share Key",
	"Integration Request",
	"Unhandled Email",
	"Webhook Request Log",
	"Workspace",
	"Route History",
	"Access Log",
]

# Request Hooks
before_request = [
	"jingrow.recorder.record",
	"jingrow.monitor.start",
	"jingrow.rate_limiter.apply",
]

after_request = [
	"jingrow.monitor.stop",
]

# Background Job Hooks
before_job = [
	"jingrow.recorder.record",
	"jingrow.monitor.start",
]

if os.getenv("JINGROW_SENTRY_DSN") and (
	os.getenv("ENABLE_SENTRY_DB_MONITORING")
	or os.getenv("SENTRY_TRACING_SAMPLE_RATE")
	or os.getenv("SENTRY_PROFILING_SAMPLE_RATE")
):
	before_request.append("jingrow.utils.sentry.set_sentry_context")
	before_job.append("jingrow.utils.sentry.set_sentry_context")

after_job = [
	"jingrow.recorder.dump",
	"jingrow.monitor.stop",
	"jingrow.utils.file_lock.release_document_locks",
]

extend_bootinfo = [
	"jingrow.utils.telemetry.add_bootinfo",
	"jingrow.core.pagetype.user_permission.user_permission.send_user_permissions",
]

get_changelog_feed = "jingrow.desk.pagetype.changelog_feed.changelog_feed.get_feed"

export_python_type_annotations = True

standard_navbar_items = [
	{
		"item_label": "My Settings",
		"item_type": "Action",
		"action": "jingrow.ui.toolbar.route_to_user()",
		"is_standard": 1,
	},
	{
		"item_label": "Reload",
		"item_type": "Action",
		"action": "jingrow.ui.toolbar.clear_cache()",
		"is_standard": 1,
	},
	{
		"item_label": "Toggle Theme",
		"item_type": "Action",
		"action": "new jingrow.ui.ThemeSwitcher().show()",
		"is_standard": 1,
	},
	{
		"item_type": "Separator",
		"is_standard": 1,
		"item_label": "",
	},
	{
		"item_label": "Log out",
		"item_type": "Action",
		"action": "jingrow.app.logout()",
		"is_standard": 1,
	},
]

standard_help_items = [
	{
		"item_label": "About",
		"item_type": "Action",
		"action": "jingrow.ui.toolbar.show_about()",
		"is_standard": 1,
	},
	{
		"item_label": "Keyboard Shortcuts",
		"item_type": "Action",
		"action": "jingrow.ui.toolbar.show_shortcuts(event)",
		"is_standard": 1,
	},
]

# log pagetype cleanups to automatically add in log settings
default_log_clearing_pagetypes = {
	"Error Log": 14,
	"Email Queue": 30,
	"Scheduled Job Log": 7,
	"Submission Queue": 7,
	"Prepared Report": 14,
	"Webhook Request Log": 30,
	"Unhandled Email": 30,
	"Reminder": 30,
	"Integration Request": 90,
	"System Log": 90,
	"Route History": 90,
}

# These keys will not be erased when doing jingrow.clear_cache()
persistent_cache_keys = [
	"changelog-*",  # version update notifications
	"insert_queue_for_*",  # Deferred Insert
	"recorder-*",  # Recorder
	"global_search_queue",
	"monitor-transactions",
	"rate-limit-counter-*",
	"rl:*",
]
