from jcloud.api.account import get_jingrow_io_auth_url

from . import __version__ as app_version

app_name = "jcloud"
app_title = "Jcloud"
app_publisher = "Jingrow"
app_description = "Managed Jingrow Hosting"
app_icon = "octicon octicon-rocket"
app_color = "grey"
app_email = "aditya@framework.jingrow.com"
app_license = "GNU Affero General Public License v3.0"
version = app_version

# Includes in <head>
# ------------------

# include js, css files in header of desk.html
# app_include_css = "/assets/jcloud/css/jcloud.css"
# app_include_js = "/assets/jcloud/js/jcloud.js"
app_include_js = [
	"jcloud.bundle.js",
]

# include js, css files in header of web template
# web_include_css = "/assets/jcloud/css/jcloud.css"
# web_include_js = "/assets/jcloud/js/jcloud.js"

# include js in page
# page_js = {"page" : "public/js/file.js"}

# include js in pagetype views
# pagetype_js = {"pagetype" : "public/js/pagetype.js"}
# pagetype_list_js = {"pagetype" : "public/js/pagetype_list.js"}
# pagetype_tree_js = {"pagetype" : "public/js/pagetype_tree.js"}
# pagetype_calendar_js = {"pagetype" : "public/js/pagetype_calendar.js"}

# Home Pages
# ----------

# application home page (will override Website Settings)
# home_page = "login"

# website user home page (by Role)
# role_home_page = {
# 	"Role": "home_page"
# }

# Website user home page (by function)
# get_website_user_home_page = "jcloud.utils.get_home_page"

# Generators
# ----------

# automatically create page for each record of this pagetype
# website_generators = ["Web Page"]

look_for_sidebar_json = True

base_template_map = {
	r"docs.*": "templates/pg.html",
	r"internal.*": "templates/pg.html",
}

update_website_context = ["jcloud.overrides.update_website_context"]

website_route_rules = [
	{"from_route": "/dashboard/<path:app_path>", "to_route": "dashboard"},
]

website_redirects = [
	{"source": "/dashboard/f-login", "target": get_jingrow_io_auth_url() or "/"},
	{
		"source": "/suspended-site",
		"target": "/api/action/jcloud.api.handle_suspended_site_redirection",
	},
	{"source": "/f-login", "target": "/dashboard/f-login"},
	{"source": "/signup", "target": "/jerp/signup"},
]

email_css = ["/assets/jcloud/css/email.css"]

jinja = {
	"filters": ["jcloud.jcloud.pagetype.marketplace_app.utils.number_k_format"],
	"methods": ["jcloud.utils.get_country_info"],
}

# Installation
# ------------

# before_install = "jcloud.install.before_install"
after_install = "jcloud.install.after_install"
after_migrate = ["jcloud.api.account.clear_country_list_cache", "jcloud.sanity.checks"]

# Desk Notifications
# ------------------
# See jingrow.core.notifications.get_notification_config

notification_config = "jcloud.notifications.get_notification_config"

# Permissions
# -----------
# Permissions evaluated in scripted ways

permission_query_conditions = {
	"Site": "jcloud.jcloud.pagetype.site.site.get_permission_query_conditions",
	"Site Domain": ("jcloud.jcloud.pagetype.site_domain.site_domain.get_permission_query_conditions"),
	"TLS Certificate": "jcloud.jcloud.pagetype.tls_certificate.tls_certificate.get_permission_query_conditions",
	"Team": "jcloud.jcloud.pagetype.team.team.get_permission_query_conditions",
	"Subscription": ("jcloud.jcloud.pagetype.subscription.subscription.get_permission_query_conditions"),
	"Stripe Payment Method": "jcloud.jcloud.pagetype.stripe_payment_method.stripe_payment_method.get_permission_query_conditions",
	"Balance Transaction": "jcloud.jcloud.pagetype.balance_transaction.balance_transaction.get_permission_query_conditions",
	"Invoice": "jcloud.jcloud.pagetype.invoice.invoice.get_permission_query_conditions",
	"App Source": ("jcloud.jcloud.pagetype.app_source.app_source.get_permission_query_conditions"),
	"App Release": ("jcloud.jcloud.pagetype.app_release.app_release.get_permission_query_conditions"),
	"Release Group": "jcloud.jcloud.pagetype.release_group.release_group.get_permission_query_conditions",
	"Deploy Candidate": "jcloud.jcloud.pagetype.deploy_candidate.deploy_candidate.get_permission_query_conditions",
	"Deploy Candidate Difference": "jcloud.jcloud.pagetype.deploy_candidate_difference.deploy_candidate_difference.get_permission_query_conditions",
	"Deploy": "jcloud.jcloud.pagetype.deploy.deploy.get_permission_query_conditions",
	"Bench": "jcloud.jcloud.pagetype.bench.bench.get_permission_query_conditions",
	"Server": "jcloud.jcloud.pagetype.server.server.get_permission_query_conditions",
	"Database Server": "jcloud.jcloud.pagetype.database_server.database_server.get_permission_query_conditions",
	"Virtual Machine": "jcloud.jcloud.pagetype.virtual_machine.virtual_machine.get_permission_query_conditions",
	"Jcloud Webhook": "jcloud.jcloud.pagetype.jcloud_webhook.jcloud_webhook.get_permission_query_conditions",
	"Jcloud Webhook Log": "jcloud.jcloud.pagetype.jcloud_webhook_log.jcloud_webhook_log.get_permission_query_conditions",
	"SQL Playground Log": "jcloud.jcloud.pagetype.sql_playground_log.sql_playground_log.get_permission_query_conditions",
	"Site Database User": "jcloud.jcloud.pagetype.site_database_user.site_database_user.get_permission_query_conditions",
}
has_permission = {
	"Site": "jcloud.overrides.has_permission",
	"Site Domain": "jcloud.overrides.has_permission",
	"TLS Certificate": "jcloud.overrides.has_permission",
	"Team": "jcloud.jcloud.pagetype.team.team.has_permission",
	"Subscription": "jcloud.overrides.has_permission",
	"Stripe Payment Method": "jcloud.overrides.has_permission",
	"Balance Transaction": "jcloud.overrides.has_permission",
	"Invoice": "jcloud.jcloud.pagetype.invoice.invoice.has_permission",
	"App Source": "jcloud.overrides.has_permission",
	"App Release": "jcloud.jcloud.pagetype.app_release.app_release.has_permission",
	"Release Group": "jcloud.overrides.has_permission",
	"Deploy Candidate": "jcloud.overrides.has_permission",
	"Deploy Candidate Difference": "jcloud.overrides.has_permission",
	"Deploy": "jcloud.overrides.has_permission",
	"Bench": "jcloud.overrides.has_permission",
	"Server": "jcloud.overrides.has_permission",
	"Database Server": "jcloud.overrides.has_permission",
	"Jcloud Webhook": "jcloud.overrides.has_permission",
	"Jcloud Webhook Log": "jcloud.overrides.has_permission",
	"Jcloud Webhook Attempt": "jcloud.jcloud.pagetype.jcloud_webhook_attempt.jcloud_webhook_attempt.has_permission",
	"SQL Playground Log": "jcloud.overrides.has_permission",
	"Site Database User": "jcloud.overrides.has_permission",
}

# Document Events
# ---------------
# Hook on document methods and events

pg_events = {
	"Stripe Webhook Log": {
		"after_insert": [
			"jcloud.jcloud.pagetype.invoice.stripe_webhook_handler.handle_stripe_webhook_events",
			"jcloud.jcloud.pagetype.team.team.process_stripe_webhook",
		],
	},
	"Address": {"validate": "jcloud.api.billing.validate_gst"},
	"Site": {"before_insert": "jcloud.jcloud.pagetype.team.team.validate_site_creation"},
	"Marketplace App Subscription": {
		"on_update": "jcloud.jcloud.pagetype.storage_integration_subscription.storage_integration_subscription.create_after_insert",
	},
}

# Scheduled Tasks
# ---------------

scheduler_events = {
	"weekly_long": ["jcloud.jcloud.pagetype.marketplace_app.events.auto_review_for_missing_steps"],
	"daily": [
		"jcloud.experimental.pagetype.referral_bonus.referral_bonus.credit_referral_bonuses",
		"jcloud.jcloud.pagetype.log_counter.log_counter.record_counts",
		"jcloud.jcloud.pagetype.incident.incident.notify_ignored_servers",
	],
	"daily_long": [
		"jcloud.jcloud.audit.check_bench_fields",
		"jcloud.jcloud.audit.check_offsite_backups",
		"jcloud.jcloud.audit.check_app_server_replica_benches",
		"jcloud.jcloud.pagetype.invoice.invoice.finalize_unpaid_prepaid_credit_invoices",
		"jcloud.jcloud.pagetype.bench.bench.sync_analytics",
		"jcloud.saas.pagetype.saas_app_subscription.saas_app_subscription.suspend_prepaid_subscriptions",
		"jcloud.jcloud.pagetype.payout_order.payout_order.create_marketplace_payout_orders",
		"jcloud.jcloud.pagetype.root_domain.root_domain.cleanup_cname_records",
		"jcloud.jcloud.pagetype.remote_file.remote_file.poll_file_statuses",
		"jcloud.jcloud.pagetype.site_domain.site_domain.update_dns_type",
		"jcloud.jcloud.pagetype.jcloud_webhook_log.jcloud_webhook_log.clean_logs_older_than_24_hours",
		"jcloud.jcloud.pagetype.virtual_disk_snapshot.virtual_disk_snapshot.sync_all_snapshots_from_aws",
	],
	"hourly": [
		"jcloud.jcloud.pagetype.site.backups.cleanup_local",
		"jcloud.jcloud.pagetype.agent_job.agent_job.update_job_step_status",
		"jcloud.jcloud.pagetype.bench.bench.archive_obsolete_benches",
		"jcloud.jcloud.pagetype.site.backups.schedule_for_sites_with_backup_time",
		"jcloud.jcloud.pagetype.tls_certificate.tls_certificate.renew_tls_certificates",
		"jcloud.saas.pagetype.product_trial_request.product_trial_request.expire_long_pending_trial_requests",
	],
	"hourly_long": [
		"jcloud.jcloud.pagetype.release_group.release_group.prune_servers_without_sites",
		"jcloud.jcloud.pagetype.server.server.scale_workers",
		"jcloud.jcloud.pagetype.usage_record.usage_record.link_unlinked_usage_records",
		"jcloud.jcloud.pagetype.bench.bench.sync_benches",
		"jcloud.jcloud.pagetype.invoice.invoice.finalize_draft_invoices",
		"jcloud.jcloud.pagetype.app.app.poll_new_releases",
		"jcloud.jcloud.pagetype.agent_job.agent_job.fail_old_jobs",
		"jcloud.jcloud.pagetype.site_update.site_update.mark_stuck_updates_as_fatal",
		"jcloud.jcloud.pagetype.deploy_candidate.deploy_candidate.cleanup_build_directories",
		"jcloud.jcloud.pagetype.deploy_candidate.deploy_candidate.delete_draft_candidates",
		"jcloud.jcloud.pagetype.deploy_candidate.deploy_candidate.check_builds_status",
		"jcloud.jcloud.pagetype.virtual_machine.virtual_machine.snapshot_virtual_machines",
		"jcloud.jcloud.pagetype.virtual_disk_snapshot.virtual_disk_snapshot.delete_old_snapshots",
		"jcloud.jcloud.pagetype.app_release.app_release.cleanup_unused_releases",
		"jcloud.jcloud.pagetype.jcloud_webhook.jcloud_webhook.auto_disable_high_delivery_failure_webhooks",
		"jcloud.saas.pagetype.product_trial.product_trial.sync_product_site_users",
	],
	"all": [
		"jcloud.auth.flush",
		"jcloud.jcloud.pagetype.site.sync.sync_setup_wizard_status",
		"jcloud.jcloud.pagetype.site.archive.archive_suspended_trial_sites",
		"jcloud.jcloud.pagetype.agent_job.agent_job.flush",
	],
	"cron": {
		"1-59/2 * * * *": [
			"jcloud.jcloud.pagetype.incident.incident.validate_incidents",
		],
		"*/2 * * * *": [
			"jcloud.jcloud.pagetype.incident.incident.resolve_incidents",
		],
		"0 4 * * *": [
			"jcloud.jcloud.pagetype.site.backups.cleanup_offsite",
			"jcloud.jcloud.cleanup.unlink_remote_files_from_site",
		],
		"10 0 * * *": [
			"jcloud.jcloud.audit.check_backup_records",
		],
		"0 3 * * *": [
			"jcloud.jcloud.pagetype.drip_email.drip_email.send_drip_emails",
		],
		"* * * * * 0/5": [
			"jcloud.jcloud.pagetype.agent_job.agent_job.poll_pending_jobs",
			"jcloud.jcloud.pagetype.jcloud_webhook_log.jcloud_webhook_log.process",
			"jcloud.jcloud.pagetype.telegram_message.telegram_message.send_telegram_message",
		],
		"0 */6 * * *": [
			"jcloud.jcloud.pagetype.server.server.cleanup_unused_files",
			"jcloud.jcloud.pagetype.razorpay_payment_record.razorpay_payment_record.fetch_pending_payment_orders",
		],
		"30 * * * *": ["jcloud.jcloud.pagetype.agent_job.agent_job.suspend_sites"],
		"*/15 * * * *": [
			"jcloud.jcloud.pagetype.site_update.site_update.schedule_updates",
			"jcloud.jcloud.pagetype.drip_email.drip_email.send_welcome_email",
			"jcloud.jcloud.pagetype.site.backups.schedule",
			"jcloud.jcloud.pagetype.site_update.site_update.run_scheduled_updates",
			"jcloud.jcloud.pagetype.site_migration.site_migration.run_scheduled_migrations",
			"jcloud.jcloud.pagetype.version_upgrade.version_upgrade.run_scheduled_upgrades",
			"jcloud.jcloud.pagetype.virtual_machine.virtual_machine.sync_virtual_machines",
			"jcloud.jcloud.pagetype.mariadb_stalk.mariadb_stalk.fetch_stalks",
		],
		"*/5 * * * *": [
			"jcloud.jcloud.pagetype.version_upgrade.version_upgrade.update_from_site_update",
			"jcloud.jcloud.pagetype.site_replication.site_replication.update_from_site",
			"jcloud.jcloud.pagetype.virtual_disk_snapshot.virtual_disk_snapshot.sync_snapshots",
			"jcloud.jcloud.pagetype.site.site.sync_sites_setup_wizard_complete_status",
		],
		"* * * * *": [
			"jcloud.jcloud.pagetype.deploy_candidate.deploy_candidate.run_scheduled_builds",
			"jcloud.jcloud.pagetype.agent_request_failure.agent_request_failure.remove_old_failures",
			"jcloud.saas.pagetype.site_access_token.site_access_token.cleanup_expired_access_tokens",
		],
		"*/10 * * * *": [
			"jcloud.saas.pagetype.product_trial.product_trial.replenish_standby_sites",
			"jcloud.jcloud.pagetype.site.saas_pool.create",
		],
		"*/30 * * * *": [
			"jcloud.jcloud.pagetype.site_update.scheduled_auto_updates.trigger",
			"jcloud.jcloud.pagetype.team.suspend_sites.execute",
		],
		"15,45 * * * *": [
			"jcloud.jcloud.pagetype.site.site_usages.update_cpu_usages",
			"jcloud.jcloud.pagetype.site.site_usages.update_disk_usages",
		],
		"15 2,4 * * *": [
			"jcloud.jcloud.pagetype.team_deletion_request.team_deletion_request.process_team_deletion_requests",
		],
		"0 0 1 */3 *": ["jcloud.jcloud.pagetype.backup_restoration_test.backup_test.run_backup_restore_test"],
		"0 8 * * *": [
			"jcloud.jcloud.cleanup.reset_large_output_fields_from_ansible_tasks",
		],
		"0 21 * * *": [
			"jcloud.jcloud.audit.billing_audit",
			"jcloud.jcloud.audit.partner_billing_audit",
		],
		"0 6 * * *": [
			"jcloud.jcloud.audit.suspend_sites_with_disabled_team",
			"jcloud.jcloud.pagetype.tls_certificate.tls_certificate.retrigger_failed_wildcard_tls_callbacks",
			"jcloud.infrastructure.pagetype.ssh_access_audit.ssh_access_audit.run",
		],
		"0 1 * * *": [
			"jcloud.jcloud.pagetype.site.site.deactivate_expired_sites",
		],
		"0 10 * * *": [
			"jcloud.jcloud.pagetype.site.site.send_renew_notification",
		],
	},
}

deploy_hours = [1, 2, 3, 4, 5, 21, 22, 23]  # Purposefully avoiding 0

fixtures = [
	"Agent Job Type",
	"Jcloud Job Type",
	"Jingrow Version",
	"MariaDB Variable",
	"Cloud Region",
	{"dt": "Role", "filters": [["role_name", "like", "Jcloud%"]]},
	"Site Config Key Blacklist",
	"Jcloud Method Permission",
	"Bench Dependency",
	"Server Storage Plan",
	"Jcloud Webhook Event",
]
# Testing
# -------

before_tests = "jcloud.tests.before_test.execute"

# Overriding Methods
# ------------------------------
#
override_whitelisted_methods = {"upload_file": "jcloud.overrides.upload_file"}
#
# each overriding function accepts a `data` argument;
# generated from the base implementation of the pagetype dashboard,
# along with any modifications made in other Jingrow apps
# override_pagetype_dashboards = {
# 	"Task": "jcloud.task.get_dashboard_data"
# }

override_pagetype_class = {"User": "jcloud.overrides.CustomUser"}

on_session_creation = "jcloud.overrides.on_session_creation"
# on_logout = "jcloud.overrides.on_logout"

before_request = "jcloud.overrides.before_request"
before_job = "jcloud.overrides.before_job"
after_job = "jcloud.overrides.after_job"

# Data Deletion Privacy Docs

user_data_fields = [
	{"pagetype": "Team", "strict": True},
]

auth_hooks = ["jcloud.auth.hook"]

page_renderer = ["jcloud.metrics.MetricsRenderer"]

export_python_type_annotations = True


# These are used for some business logic, they should be manually evicted.
__persistent_cache_keys = [
	"agent-jobs",
	"monitor-transactions",
	"google_oauth_flow*",
	"fc_oauth_state*",
	"one_time_login_key*",
	"jcloud-auth-logs",
	"rl:*",
]

# `jingrow.rename_pg` erases all caches, this hook preserves some of them.
# Note:
# - These are only "most used" cache keys. This lessens the impact of renames but doesn't eliminate them.
# - Adding more keys here will slow down `jingrow.clear_cache` but it's "rare" enough.
# - This also means that other "valid" jingrow.clear_cache() usage won't clear these keys!
# - Use jingrow.cache.flushall() instead.
persistent_cache_keys = [
	*__persistent_cache_keys,
	"agent_job_step_output",
	"all_apps",
	"app_hooks",
	"assets_json",
	"assignment_rule_map",
	"bootinfo",
	"builder.builder*",  # path resolution, it has its own cache eviction.
	"db_tables",
	"defaults",
	"pagetype_form_meta",
	"pagetype_meta",
	"pagetypes_with_web_view",
	"document_cache::*",
	"document_naming_rule_map",
	"domain_restricted_pagetypes",
	"domain_restricted_pages",
	"energy_point_rule_map",
	"jingrow.utils.scheduler.schedule_jobs_based_on_activity*",  # dormant checks
	"jingrow.website.page_renderers*",  # FW's routing
	"home_page",
	"information_schema:counts",
	"installed_app_modules",
	"ip_country_map",
	"is_table",
	"languages",
	"last_db_session_update",
	"marketplace_apps",
	"merged_translations",
	"metadata_version",
	"server_script_map",  # Routing and actual server scripts
	"session",
	"table_columns",
	"website_page",
	"website_route_rules",
]

before_migrate = ["jcloud.overrides.before_after_migrate"]
after_migrate = ["jcloud.overrides.before_after_migrate"]
