"""
Social Media Boosting — Flask blueprint only; business logic lives in social_boosting_client.py.
"""

from __future__ import annotations

from flask import Blueprint, redirect, render_template, session

import models
from social_boosting_buckets import annotate_services_platforms, tabs_for_catalog
from social_boosting_client import fetch_panel_snapshot
from social_boosting_pricing import usd_to_mmk_rate

social_boosting_bp = Blueprint(
    "social_boosting",
    __name__,
    template_folder="templates",
)


@social_boosting_bp.route("/social-boosting")
def social_boosting_page():
    uid = session.get("user_id")
    if not uid:
        return redirect("/login")
    user = models.get_user(uid)
    if not user:
        return redirect("/login")
    balance = models.get_balance(uid)
    snapshot = fetch_panel_snapshot()
    svcs = snapshot.get("services") or []
    if isinstance(svcs, list):
        annotate_services_platforms(svcs)
        sb_tabs = tabs_for_catalog(svcs)
    else:
        sb_tabs = ["All"]
    return render_template(
        "social_boosting.html",
        user=user,
        balance=balance,
        snapshot=snapshot,
        sb_usd_to_mmk=usd_to_mmk_rate(),
        sb_tabs=sb_tabs,
    )
