# server/wsgi.py
from server.routes.eventmedia import eventmedia_bp
from server.routes.files import files_bp
from server.routes.events import events_bp
from server.routes.event_exceptions import event_exceptions_bp
from server.routes.conversions import conversions_bp
from server.routes.holidays import holidays_bp
from server.routes.academic_periods import academic_periods_bp
from server.routes.groups import groups_bp
from server.routes.clients import clients_bp
from server.routes.client_logs import client_logs_bp
from server.routes.auth import auth_bp
from server.routes.users import users_bp
from server.routes.system_settings import system_settings_bp
from server.database import Session, engine
from flask import Flask, jsonify, send_from_directory, request
import glob
import os
import sys
sys.path.append('/workspace')

app = Flask(__name__)

# Allow uploads up to 1 GiB at the Flask level (application hard limit)
# See nginx.conf for proxy limit; keep both in sync.
app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024 * 1024  # 1 GiB

# Configure Flask session
# In production, use a secure random key from environment variable
app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY', 'dev-secret-key-change-in-production')
app.config['SESSION_COOKIE_HTTPONLY'] = True
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.register_blueprint(system_settings_bp)
# In production, set to True if using HTTPS
app.config['SESSION_COOKIE_SECURE'] = os.environ.get('ENV', 'development').lower() == 'production'
# Session lifetime: longer in development for convenience
from datetime import timedelta
_env = os.environ.get('ENV', 'development').lower()
if _env in ('development', 'dev'):
    app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=30)
else:
    # Keep modest in production; can be tuned via env later
    app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=1)

# Blueprints importieren und registrieren

app.register_blueprint(auth_bp)
app.register_blueprint(users_bp)
app.register_blueprint(clients_bp)
app.register_blueprint(client_logs_bp)
app.register_blueprint(groups_bp)
app.register_blueprint(events_bp)
app.register_blueprint(event_exceptions_bp)
app.register_blueprint(eventmedia_bp)
app.register_blueprint(files_bp)
app.register_blueprint(holidays_bp)
app.register_blueprint(academic_periods_bp)
app.register_blueprint(conversions_bp)


@app.route("/health")
def health():
    return jsonify(status="ok")


@app.route("/")
def index():
    return "Hello from Infoscreen‐API!"


@app.route("/screenshots/<uuid>/priority")
def get_priority_screenshot(uuid):
    normalized_uuid = uuid[:-4] if uuid.lower().endswith('.jpg') else uuid
    priority_filename = f"{normalized_uuid}_priority.jpg"
    priority_path = os.path.join("screenshots", priority_filename)
    if os.path.exists(priority_path):
        return send_from_directory("screenshots", priority_filename)
    return get_screenshot(uuid)


@app.route("/screenshots/<uuid>")
@app.route("/screenshots/<uuid>.jpg")
def get_screenshot(uuid):
    normalized_uuid = uuid[:-4] if uuid.lower().endswith('.jpg') else uuid
    latest_filename = f"{normalized_uuid}.jpg"
    latest_path = os.path.join("screenshots", latest_filename)
    if os.path.exists(latest_path):
        return send_from_directory("screenshots", latest_filename)

    pattern = os.path.join("screenshots", f"{normalized_uuid}_*.jpg")
    files = glob.glob(pattern)
    if not files:
        # Dummy-Bild als Redirect oder direkt als Response
        return jsonify({"error": "Screenshot not found", "dummy": "https://placehold.co/400x300?text=No+Screenshot"}), 404
    files.sort(reverse=True)
    filename = os.path.basename(files[0])
    return send_from_directory("screenshots", filename)


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000, debug=True)
