from flask import Flask, request, jsonify
from topcinema import TopCinemaAPI

app = Flask(__name__)
api = TopCinemaAPI()

@app.route('/api/movie', methods=['GET'])
def get_movie():
    movie_url = request.args.get('movie_url')
    if not movie_url:
        return jsonify({
            "error": "movie_url parameter is required",
            "success": False
        }), 400
    
    return jsonify(api.get_movie_info(movie_url))

@app.route('/api/series', methods=['GET'])
def get_series():
    series_url = request.args.get('series_url')
    if not series_url:
        return jsonify({
            "error": "series_url parameter is required",
            "success": False
        }), 400
    
    return jsonify(api.get_series_info(series_url))

@app.route('/api/play', methods=['GET'])
def get_play():
    watch_url = request.args.get('watch_url')
    if not watch_url:
        return jsonify({
            "error": "watch_url parameter is required",
            "success": False
        }), 400
    
    play_url = api._get_final_play_url(watch_url)
    
    if play_url:
        return jsonify({
            "play_url": play_url,
            "success": True
        })
    else:
        return jsonify({
            "error": "Could not extract play URL",
            "success": False
        }), 404

if __name__ == '__main__':
    app.run(debug=True)