# -*- coding: utf-8 -*-
"""
service_manage.py — 服务管理模块路由

页面路由：/service-manage/*
  - /service-manage              → 重定向到服务授权
  - /service-manage/authorization → 服务授权
  - /service-manage/upgrade       → 服务升级
  - /service-manage/info          → 服务信息

权限：仅管理员可访问
"""

from flask import Blueprint, render_template, session, redirect, url_for

from decorators import page_login_required
from utils.logger import get_logger

logger = get_logger(__name__)

bp = Blueprint('service-manage', __name__)


# ============================================================
# 内部辅助
# ============================================================

def _current_user():
    return session.get('user', {})


def _is_admin():
    return _current_user().get('role') == 'admin'


# ============================================================
# 页面路由
# ============================================================

@bp.route('/service-manage')
@page_login_required
def page_index():
    """服务管理主页 → 重定向到服务授权"""
    if not _is_admin():
        return redirect('/')
    return redirect(url_for('service-manage.page_authorization'))


@bp.route('/service-manage/authorization')
@page_login_required
def page_authorization():
    """服务授权页面"""
    if not _is_admin():
        return redirect('/')
    user = _current_user()
    return render_template(
        'service_manage/authorization.html',
        user=user,
        is_admin=True,
        active_menu='authorization',
    )


@bp.route('/service-manage/upgrade')
@page_login_required
def page_upgrade():
    """服务升级页面"""
    if not _is_admin():
        return redirect('/')
    user = _current_user()
    return render_template(
        'service_manage/upgrade.html',
        user=user,
        is_admin=True,
        active_menu='upgrade',
    )


@bp.route('/service-manage/info')
@page_login_required
def page_info():
    """服务信息页面"""
    if not _is_admin():
        return redirect('/')
    user = _current_user()
    return render_template(
        'service_manage/info.html',
        user=user,
        is_admin=True,
        active_menu='info',
    )
