#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations

from flask import Blueprint, jsonify

import database
from services.time_utils import get_beijing_now

health_bp = Blueprint("health", __name__)


@health_bp.route("/health", methods=["GET"])
def health_check():
    """轻量健康检查：用于宿主机/负载均衡探活。"""
    db_ok = True
    db_error = ""
    try:
        database.get_system_config()
    except Exception as e:
        db_ok = False
        db_error = f"{type(e).__name__}: {e}"

    payload = {
        "ok": db_ok,
        "time": get_beijing_now().strftime("%Y-%m-%d %H:%M:%S"),
        "db_ok": db_ok,
        "db_error": db_error,
    }
    return jsonify(payload), (200 if db_ok else 500)

