from flask import Flask
from flask_restful import Api
from flask_jwt_extended import JWTManager

from ma import ma
from db import db
from resources.user import UserRegister, UserLogin, UserLogout
from resources.homepage import Homepage
from resources.car import Car, CarList
from blacklist import BLACKLIST


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['JWT_BLACKLIST_ENABLED'] = True
app.config["JWT_BLACKLIST_TOKEN_CHECKS"] = [
    "access",
    "refresh",
]  # allow blacklisting for access and refresh tokens
app.secret_key = 'saphir09' # could do app.config['JWT_SECRET_KEY'] if we prefer
api = Api(app)


@app.before_first_request
def create_tables():
    db.create_all()


jwt = JWTManager(app)

# This method will check if a token is blacklisted, and will be called automatically when blacklist is enabled
@jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
    return decrypted_token["jti"] in BLACKLIST

# @jwt.expired_token_loader
# def expired_token_callback():
#     return jsonify({
#         'description': 'The token has expired.',
#         'error': 'token expired'
#     }), 401

# @jwt.invalid_token_loader
# def invalid_token_callback(error):
#     return jsonify({
#         'description': 'Signature verification failed.',
#         'error': 'invalid_token'
#     }), 401

# @jwt.unauthorized_loader
# def missing_token_callback(error):
#     return jsonify({
#         'description': 'Request does not contain an access token.',
#         'error': 'authorization_required'
#     }), 401

# @jwt.revoked_token_loader
# def revoked_token_callback():
#     return jsonify({
#         'description': 'The token has been revoked.',
#         'error': 'token_revoked'
#     }), 401

api.add_resource(Homepage, '/')
api.add_resource(UserRegister, '/register')
api.add_resource(UserLogin, '/login')
api.add_resource(UserLogout, '/logout')

api.add_resource(Car, "/car/<string:carname>")
api.add_resource(CarList, "/cars")

if __name__ == "__main__":
    db.init_app(app)
    ma.init_app(app)
    app.run(port=5000, debug=True)
