"""Admin dashboard blueprint."""
from flask import Blueprint, jsonify
from blueprints.auth_bp import require_manager_auth
from services import tenant_service, migration_service

admin_bp = Blueprint("admin", __name__, url_prefix="/api/admin")


@admin_bp.route("/stats", methods=["GET"])
@require_manager_auth
def dashboard_stats():
    return jsonify(tenant_service.get_dashboard_stats())


@admin_bp.route("/migrations", methods=["GET"])
@require_manager_auth
def list_migrations():
    return jsonify({
        "migrations": migration_service.list_available_migrations(),
        "tenants": migration_service.get_tenant_versions()
    })


@admin_bp.route("/migrations/run-all", methods=["POST"])
@require_manager_auth
def run_all_migrations():
    result = migration_service.run_all_pending_migrations()
    return jsonify(result)


@admin_bp.route("/migrations/run/<version>", methods=["POST"])
@require_manager_auth
def run_specific_migration(version):
    result = migration_service.run_migration_on_all_tenants(version)
    return jsonify({"results": result})
