from flask import Blueprint, render_template, request, send_file, jsonify
import math, time
from helpers.db import db_get_videos, db_get_video, db_get_recent
from helpers.favorites import mark_favorites, db_get_favorites, db_get_fav_set
from helpers.cache import build_cache
from config import VIDEOS_PER_PAGE, DASHBOARD_PER_PAGE

web = Blueprint("web", __name__)

@web.route("/")
def dashboard():
    cache = build_cache()
    query = request.args.get("q", "").lower().strip()
    sort = request.args.get("sort", "total_size")
    dir_ = request.args.get("dir", "desc")
    reverse = (dir_ == "desc")

    items = list(cache["storage_usage"].items())

    if query:
        items = [e for e in items if query in e[0].split("::")[0].lower()]

    def k_user(x): return x[0].split("::")[0].lower()
    def k_platform(x): return x[0].split("::")[1].lower()
    def k_total(x): return x[1]["total_size"]
    def k_count(x): return x[1]["video_count"]
    def k_avg(x): return cache["avg_sizes"][x[0]]

    key_map = {
        "user": k_user,
        "platform": k_platform,
        "total_size": k_total,
        "video_count": k_count,
        "avg_size": k_avg,
    }
    items.sort(key=key_map.get(sort, k_total), reverse=reverse)

    page = max(1, int(request.args.get("page", 1)))
    total_pages = max(1, math.ceil(len(items) / DASHBOARD_PER_PAGE))
    start = (page - 1) * DASHBOARD_PER_PAGE
    paginated = items[start:start + DASHBOARD_PER_PAGE]

    return render_template(
        "main.html",
        storage_usage=paginated,
        avg_sizes=cache["avg_sizes"],
        page=page,
        total_pages=total_pages,
        query=query,
        sort=sort,
        dir=dir_
    )

@web.route("/refresh")
def refresh():
    cache = build_cache()
    return jsonify({
        "status": "ok",
        "videos": sum(x["video_count"] for x in cache["storage_usage"].values()),
        "updated": time.ctime(cache["timestamp"])
    })

@web.route("/user/<username>")
def user_page(username):
    videos = db_get_videos(username=username)
    page = max(1, int(request.args.get("page", 1)))
    total_pages = max(1, math.ceil(len(videos) / VIDEOS_PER_PAGE))
    start = (page - 1) * VIDEOS_PER_PAGE
    paginated = videos[start:start + VIDEOS_PER_PAGE]
    mark_favorites(paginated)

    return render_template(
        "user_page.html",
        username=username,
        videos=paginated,
        page=page,
        total_pages=total_pages
    )

@web.route("/video/stream/<video_id>")
def stream_video(video_id):
    video = db_get_video(video_id)
    if video:
        return send_file(video["filepath"], mimetype="video/mp4")
    return "Video not found", 404

@web.route("/video/<video_id>")
def view_video(video_id):
    video = db_get_video(video_id)
    if video:
        video["is_favorite"] = (video["video_id"] in db_get_fav_set())
        return render_template("video_view.html", video=video)
    return "Video not found", 404

@web.route("/recent")
def recent():
    page = max(1, int(request.args.get("page", 1)))
    per_page = VIDEOS_PER_PAGE
    videos, total = db_get_recent(page, per_page)
    total_pages = max(1, math.ceil(total / per_page))
    mark_favorites(videos)

    return render_template(
        "recent.html",
        videos=videos,
        page=page,
        total_pages=total_pages
    )

@web.route("/favorites")
def favorites_page():
    page = max(1, int(request.args.get("page", 1)))
    per_page = VIDEOS_PER_PAGE
    videos, total = db_get_favorites(page, per_page)
    total_pages = max(1, math.ceil(total / per_page))
    mark_favorites(videos)

    return render_template(
        "favorites.html",
        videos=videos,
        page=page,
        total_pages=total_pages
    )