from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager

# from authlib.integrations.flask_client import OAuth

# Web Server
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.sqlite"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SECRET_KEY"] = "bvjchsygvduycgsyugc"


# ORM
db = SQLAlchemy(app)

migrate = Migrate(app, db)

# Login manager
from application.auth.models import User

login_manager = LoginManager()
login_manager.init_app(app)  # type: ignore
login_manager.login_view = "auth.login"  # type: ignore


# Gets all the user data
@login_manager.user_loader  # type: ignore
def load_user(user_id):  # type: ignore
    return User.query.get(int(user_id))  # type: ignore


# Keycloak
"""
oauth = OAuth(app=app)
url = "http://192.168.69.1:8180"

keycloak: ... = oauth.register(
    name="keycloak",
    client_id="ProjectIOT",
    client_secret="IWKfsx2aLHCMr0iUaZOuws6UwiYrVQ60",
    authorize_url=(f"{url}/realms/ProjectIOT/protocol/openid-connect/auth"),
    authorize_params=None,
    access_token_url=(
        f"{url}/realms/ProjectIOT/protocol/openid-connect/token"
    ),
    refresh_token_url=(
        f"{url}/realms/ProjectIOT/protocol/openid-connect/token"
    ),
    api_base_url=(f"{url}/realms/ProjectIOT/protocol/openid-connect"),
    client_kwargs={"scope": "openid profile email"},
    server_metadata_url=(
        "{url}/realms/ProjectIOT/.well-known/openid-configuration"
    ),
)
"""
