from flask import Flask
from flask_cors import CORS

from api import register_blueprints


def create_app():
    app = Flask(__name__)
    
    CORS(app)
    
    register_blueprints(app)
    
    @app.route('/')
    def index():
        return {
            'code': 200,
            'message': 'China Enterprise Absence Analysis System API',
            'data': {
                'version': '1.0.0',
                'endpoints': {
                    'overview': [
                        '/api/overview/stats',
                        '/api/overview/trend',
                        '/api/overview/weekday',
                        '/api/overview/reasons',
                        '/api/overview/seasons'
                    ],
                    'analysis': [
                        '/api/analysis/importance',
                        '/api/analysis/correlation',
                        '/api/analysis/compare'
                    ],
                    'predict': [
                        '/api/predict/single',
                        '/api/predict/model-info'
                    ],
                    'cluster': [
                        '/api/cluster/result',
                        '/api/cluster/profile',
                        '/api/cluster/scatter'
                    ],
                    'jdr': [
                        '/api/jdr/dimensions',
                        '/api/jdr/burnout-engagement',
                        '/api/jdr/path-analysis',
                        '/api/jdr/profile',
                        '/api/jdr/risk-distribution'
                    ],
                    'shap': [
                        '/api/shap/global',
                        '/api/shap/local',
                        '/api/shap/interaction',
                        '/api/shap/dependence'
                    ]
                }
            }
        }
    
    @app.errorhandler(404)
    def not_found(e):
        return {
            'code': 404,
            'message': 'Resource not found',
            'data': None
        }, 404
    
    @app.errorhandler(500)
    def server_error(e):
        return {
            'code': 500,
            'message': 'Internal server error',
            'data': None
        }, 500
    
    return app


if __name__ == '__main__':
    app = create_app()
    app.run(host='0.0.0.0', port=5000, debug=True)
