from flask import Blueprint, jsonify

from services.data_service import data_service

overview_bp = Blueprint('overview', __name__, url_prefix='/api/overview')


@overview_bp.route('/stats', methods=['GET'])
def get_stats():
    try:
        stats = data_service.get_basic_stats()
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': stats
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@overview_bp.route('/trend', methods=['GET'])
def get_trend():
    try:
        trend = data_service.get_monthly_trend()
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': trend
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@overview_bp.route('/weekday', methods=['GET'])
def get_weekday():
    try:
        weekday = data_service.get_weekday_distribution()
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': weekday
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@overview_bp.route('/reasons', methods=['GET'])
def get_reasons():
    try:
        reasons = data_service.get_reason_distribution()
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': reasons
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500


@overview_bp.route('/seasons', methods=['GET'])
def get_seasons():
    try:
        seasons = data_service.get_season_distribution()
        return jsonify({
            'code': 200,
            'message': 'success',
            'data': seasons
        })
    except Exception as e:
        return jsonify({
            'code': 500,
            'message': str(e),
            'data': None
        }), 500
