from my_modules.functions import (
  custom_limit_key,
  get_my_ip_address,
  get_local_ip_addresses,
  replace_last_ip_segment,
  generate_all_ips,
)
from my_modules.app.constens import SECRET_KEY, THE_IP_BOT_MANAGER
from my_modules.OrphanStorageIdRegistry import OrphanStorageIdRegistry
from my_modules.AsyncCache import AsyncCache
from my_modules.app.logger import logger
from quart_common.web.request import TRUST_PROXY_HEADERS, TRUSTED_PROXY_IPS, TRUSTED_PROXY_CIDRS
from quart_common.web.wide_event import register_wide_event_logging

from my_helpers.db.convex.ConvexRuntime import ConvexRuntime
from my_helpers.db.convex.ConvexWorkerPool import ConvexWorkerPool
from my_modules.db.ConvexDB import ConvexDB

from quart_session import Session
from flask_limiter import Limiter

import redis.asyncio as aioredis
from quart import Quart
import os

app = Quart(__name__,
  template_folder="../../templates/side",
  static_folder="../../templates/static",
)
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 1024
register_wide_event_logging(app, logger)

app.secret_key = SECRET_KEY

# Cache, Sessions and Limiter over Valkey
if os.getenv("VALKEY_HOST", None) is not None:
  cache = AsyncCache(
    backend='redis',
    default_ttl=300,
    username=os.getenv('VALKEY_CACHE_USER', ''),
    password=os.getenv('VALKEY_CACHE_PASSWORD', ''),
    host=os.getenv('VALKEY_HOST'),
    port=os.getenv('VALKEY_PORT', 6379),
    db=os.getenv('VALKEY_DB', 0),
  )
else:
  cache = AsyncCache(
    backend='memory',
  )

if os.getenv("VALKEY_HOST", None) is not None:
  app.config.from_mapping(
    SESSION_TYPE='redis',
    SESSION_PERMANENT=True,
    SESSION_USE_SIGNER=True,
    SESSION_REDIS=aioredis.Redis(
      username=os.getenv('VALKEY_SESSION_USER', None),
      password=os.getenv('VALKEY_SESSION_PASSWORD', None),
      host=os.getenv('VALKEY_HOST'),
      port=os.getenv('VALKEY_PORT', 6379),
      db=os.getenv('VALKEY_DB', 0),
      decode_responses=True,
    ),
  )
else:
  app.config.from_mapping(
    SESSION_TYPE='memcached',
  )

Session(app)

LIMITER = Limiter(
  custom_limit_key,
  app=app,
  storage_uri=(
    f'redis://{os.getenv('VALKEY_LIMITER_USER', '')}:{os.getenv('VALKEY_LIMITER_PASSWORD', '')}'
    f'@{os.getenv('VALKEY_HOST')}:{os.getenv('VALKEY_PORT', 6379)}/{os.getenv('VALKEY_DB', 0)}'
  )
  if os.getenv('VALKEY_HOST')
  else None,
  default_limits=[],
  strategy='moving-window',
)

convex_runtime = ConvexWorkerPool(os.getenv('CONVEX_URL'))
app.convex_runtime = convex_runtime

orphan_retention_seconds = max(60, int(os.getenv('UPLOAD_ORPHAN_ID_RETENTION_SECONDS', '600')))
if os.getenv('VALKEY_HOST', None) is not None:
  orphan_redis = aioredis.Redis(
    username=os.getenv('VALKEY_CACHE_USER', None),
    password=os.getenv('VALKEY_CACHE_PASSWORD', None),
    host=str(os.getenv('VALKEY_HOST')),
    port=int(os.getenv('VALKEY_PORT', 6379)),
    db=int(os.getenv('VALKEY_DB', 0)),
    decode_responses=False,
  )
else:
  orphan_redis = None

app.orphan_storage_registry = OrphanStorageIdRegistry(
  retention_seconds=orphan_retention_seconds,
  redis_client=orphan_redis,
)

@app.before_serving
async def init_convex():
  await logger.info(
    f"Proxy trust config | trust_headers={TRUST_PROXY_HEADERS} "
    f"trusted_proxy_ips={sorted(TRUSTED_PROXY_IPS)} "
    f"trusted_proxy_cidrs={[str(network) for network in TRUSTED_PROXY_CIDRS]}"
  )

  await convex_runtime.start()
  app.convex = ConvexDB(runtime=convex_runtime)

  THE_IP_BOT_MANAGER.add_always_allowed_ip('127.0.0.1')
  THE_IP_BOT_MANAGER.add_always_allowed_ip(await get_my_ip_address())

  local_docker_host_ip = get_local_ip_addresses()
  if local_docker_host_ip:
    base_ip = replace_last_ip_segment(local_docker_host_ip, 1)
    all_local_ips = generate_all_ips(base_ip)
    THE_IP_BOT_MANAGER.update_always_allowed_ip(all_local_ips)

@app.after_serving
async def close_convex():
  if app.convex:
    await convex_runtime.stop()
  orphan_registry = getattr(app, 'orphan_storage_registry', None)
  if orphan_registry:
    await orphan_registry.close()
  await logger.shutdown()
