# -*- coding: utf-8 -*-
# type: ignore
from __future__ import annotations

import os

from celery.schedules import crontab
from corsheaders.defaults import default_headers

from .base import *  # noqa: F401,F403

INSTALLED_APPS += [  # noqa: F405
    'bthlabs_jsonrpc_django',
    'corsheaders',
    'crispy_forms',
    'crispy_bootstrap5',
    'django_htmx',
]

MIDDLEWARE = [
    'hotpocket_backend.apps.core.middleware.RequestIDMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'hotpocket_backend.apps.accounts.middleware.AccessTokenMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
    'django_htmx.middleware.HtmxMiddleware',
]

TEMPLATES[0]['OPTIONS']['context_processors'] += [  # noqa: F405
    'hotpocket_backend.apps.ui.context_processors.base_includes',
    'hotpocket_backend.apps.ui.context_processors.page_includes',
]

ROOT_URLCONF = 'hotpocket_backend.urls.webapp'

LOGIN_REDIRECT_URL = '/accounts/post-login/'

SESSION_COOKIE_SAMESITE = 'None'
SESSION_COOKIE_SECURE = True

CRISPY_ALLOWED_TEMPLATE_PACKS = 'bootstrap5'
CRISPY_TEMPLATE_PACK = 'bootstrap5'

CELERY_BEAT_SCHEDULE = {
    'clean-expired-auth-keys': {
        'task': 'hotpocket_backend.apps.accounts.tasks.clean_expired_auth_keys',
        'schedule': crontab(minute='30', hour='3'),
    },
}

HOTPOCKET_BOT_STRATEGY = 'hotpocket_backend.apps.bot.strategy.basic:BasicStrategy'
HOTPOCKET_BOT_BANNED_HOSTNAMES = [
    # YT returns dummy data when I try to fetch the page and extract
    # metadata. I'd have to use Google APIs for that and it's 11:30 PM...
    'youtube.com',
    'youtu.be',
    # Reddit's response is too generic to pull any useful info from it.
    # Since they forced Apollo to shut down, I refuse to even think about
    # interacting with their API :P.
    'reddit.com',
    # Twitter, amirite?
    'twitter.com',
    't.co',
    'x.com',
]

SAVES_SAVE_ADAPTER = os.environ.get(
    'HOTPOCKET_BACKEND_SAVES_SAVE_ADAPTER',
    'hotpocket_backend.apps.saves.adapters.basic:BasicSaveAdapter',
)
SAVES_ASSOCIATION_ADAPTER = os.environ.get(
    'HOTPOCKET_BACKEND_SAVES_ASSOCIATION_ADAPTER',
    'hotpocket_backend.apps.saves.adapters.basic:BasicAssociationAdapter',
)

JSONRPC_METHOD_MODULES = [
    'hotpocket_backend.apps.ui.rpc_methods',
]

CORS_ALLOWED_ORIGIN_REGEXES = [
    r'safari-web-extension:\/\/.+?',
    r'chrome-extension:\/\/.+?',
]
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_HEADERS = (
    *default_headers,
    'cookie',
)

AUTH_KEY_TTL = 30

UI_BASE_HEAD_INCLUDES = []
UI_BASE_SCRIPT_INCLUDES = []

UI_PAGE_HEAD_INCLUDES = []
UI_PAGE_SCRIPT_INCLUDES = []
