import os
from flask import Flask, render_template, request, session, redirect
from flask_cors import CORS
from flask_migrate import Migrate
from flask_wtf.csrf import CSRFProtect, generate_csrf
from flask_login import LoginManager

from .models import db, User
from .api.user_routes import user_routes
from .api.auth_routes import auth_routes
from .api.customer_routes import customer_routes
from .api.employee_routes import employee_routes
from .api.food_or_drink_routes import food_or_drink_routes
from .api.ingredient_routes import ingredient_routes
from .api.inventory_routes import inventory_routes
from .api.menue_routes import menue_routes
from .api.qr_code_route import qr_code_routes
from .api.table_routes import table_routes
# from .api.menue_food_or_drinks_routes import menue_food_or_drinks_routes
from .api.menu_list_routes import menu_list_routes

from .seeds import seed_commands

from .config import Config

app = Flask(__name__)

# Setup login manager
login = LoginManager(app)
login.login_view = 'auth.unauthorized'


@login.user_loader
def load_user(id):
    return User.query.get(int(id))


# Tell flask about our seed commands
app.cli.add_command(seed_commands)

app.config.from_object(Config)
app.register_blueprint(user_routes, url_prefix='/api/users')
app.register_blueprint(auth_routes, url_prefix='/api/auth')
app.register_blueprint(customer_routes, url_prefix='/api/customer')
app.register_blueprint(employee_routes, url_prefix='/api/employee')
app.register_blueprint(food_or_drink_routes, url_prefix='/api/food_or_drink')
app.register_blueprint(ingredient_routes, url_prefix='/api/ingredient')
app.register_blueprint(inventory_routes, url_prefix='/api/inventory')
app.register_blueprint(menue_routes, url_prefix='/api/menue')
app.register_blueprint(qr_code_routes, url_prefix='/api/qr_code')
app.register_blueprint(table_routes, url_prefix='/api/table')
app.register_blueprint(menu_list_routes, url_prefix='/api/menu_list')
# app.register_blueprint(menue_food_or_drinks_routes,
#                        url_prefix='/api/menue_food_or_drinks')

db.init_app(app)
Migrate(app, db)

# Application Security
CORS(app)

# Since we are deploying with Docker and Flask,
# we won't be using a buildpack when we deploy to Heroku.
# Therefore, we need to make sure that in production any
# request made over http is redirected to https.
# Well.........


@app.before_request
def https_redirect():
    if os.environ.get('FLASK_ENV') == 'production':
        if request.headers.get('X-Forwarded-Proto') == 'http':
            url = request.url.replace('http://', 'https://', 1)
            code = 301
            return redirect(url, code=code)


@app.after_request
def inject_csrf_token(response):
    response.set_cookie('csrf_token',
                        generate_csrf(),
                        secure=True if os.environ.get(
                            'FLASK_ENV') == 'production' else False,
                        samesite='Strict' if os.environ.get(
                            'FLASK_ENV') == 'production' else None,
                        httponly=True)
    return response


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def react_root(path):
    print("path", path)
    if path == 'favicon.ico':
        return app.send_static_file('favicon.ico')
    return app.send_static_file('index.html')
