from sqlalchemy.orm import Session
from sqlmodel import select
from sqlalchemy.exc import SQLAlchemyError
from typing import Any

# import sys
#
# sys.path.append("..")
from app.db import models, pagination,session_scope
from app.util import passutil, schemas
from app.logs import fastapi_logger
from app.crud import get_user,get_user_password


class CRUDLogin:

    def check_username_password(self, email: str, password: str) -> Any:
        """ Verify Password"""

        db_user_info = get_user_password(email=email)

        return passutil.verify_password(str(password),
                                        str(db_user_info.password))

    def check_active_session(self, session_id: str):
        """ check for active session """
        try:
            with session_scope() as db:
                statement = select(models.UsersLoginAttempt).where(
                    models.UsersLoginAttempt.session_id == session_id)
                results = db.exec(statement)
                data = results.one()
                return data
        except SQLAlchemyError as e:
            fastapi_logger.exception("check_active_session")
            return None

    def login_user(self, user: schemas.UserLogIn, session_id: str) -> Any:
        """ Login Attempt Record """
        try:
            with session_scope() as db:
                db_session = models.UsersLoginAttempt(
                    email=user.email,
                    session_id=session_id,
                    ip_address=user.ip_address,
                    browser=user.browser,
                    status="logged_in")
                db.add(db_session)
                db.commit()
                db.refresh(db_session)
                return db_session
        except SQLAlchemyError as e:
            fastapi_logger.exception("login_user")
            return None

    def active_user(self, session_id: str) -> Any:
        """ check for active user"""
        try:
            with session_scope() as db:
                statement = select(models.UsersLoginAttempt).where(
                    models.UsersLoginAttempt.session_id == session_id)
                results = db.exec(statement)
                db_session = results.one()

                db_session.status = "active"
                db.add(db_session)
                db.commit()
                db.refresh(db_session)
                return db_session
        except SQLAlchemyError as e:
            fastapi_logger.exception("active_user")
            return None

    def logoff_user(self, session_id: str) -> Any:
        """ Logging off Record"""
        try:
            with session_scope() as db:
                statement = select(models.UsersLoginAttempt).where(
                    models.UsersLoginAttempt.session_id == session_id)
                results = db.exec(statement)
                db_session = results.one()

                db_session.status = "logged_off"
                db.add(db_session)
                db.commit()
                db.refresh(db_session)
                return db_session
        except SQLAlchemyError as e:
            fastapi_logger.exception("logoff_user")
            return None


crud_login = CRUDLogin()

app.crud.get_user_password app.db.session_scope app.logs.fastapi_logger.exception sqlmodel.select app.db.models.UsersLoginAttempt
[((511, 541), 'app.crud.get_user_password', 'get_user_password', ([], {'email': 'email'}), '(email=email)\n', (528, 541), False, 'from app.crud import get_user, get_user_password\n'), ((799, 814), 'app.db.session_scope', 'session_scope', ([], {}), '()\n', (812, 814), False, 'from app.db import models, pagination, session_scope\n'), ((1127, 1175), 'app.logs.fastapi_logger.exception', 'fastapi_logger.exception', (['"""check_active_session"""'], {}), "('check_active_session')\n", (1151, 1175), False, 'from app.logs import fastapi_logger\n'), ((1349, 1364), 'app.db.session_scope', 'session_scope', ([], {}), '()\n', (1362, 1364), False, 'from app.db import models, pagination, session_scope\n'), ((1402, 1541), 'app.db.models.UsersLoginAttempt', 'models.UsersLoginAttempt', ([], {'email': 'user.email', 'session_id': 'session_id', 'ip_address': 'user.ip_address', 'browser': 'user.browser', 'status': '"""logged_in"""'}), "(email=user.email, session_id=session_id,\n    ip_address=user.ip_address, browser=user.browser, status='logged_in')\n", (1426, 1541), False, 'from app.db import models, pagination, session_scope\n'), ((1835, 1873), 'app.logs.fastapi_logger.exception', 'fastapi_logger.exception', (['"""login_user"""'], {}), "('login_user')\n", (1859, 1873), False, 'from app.logs import fastapi_logger\n'), ((2023, 2038), 'app.db.session_scope', 'session_scope', ([], {}), '()\n', (2036, 2038), False, 'from app.db import models, pagination, session_scope\n'), ((2516, 2555), 'app.logs.fastapi_logger.exception', 'fastapi_logger.exception', (['"""active_user"""'], {}), "('active_user')\n", (2540, 2555), False, 'from app.logs import fastapi_logger\n'), ((2702, 2717), 'app.db.session_scope', 'session_scope', ([], {}), '()\n', (2715, 2717), False, 'from app.db import models, pagination, session_scope\n'), ((3199, 3238), 'app.logs.fastapi_logger.exception', 'fastapi_logger.exception', (['"""logoff_user"""'], {}), "('logoff_user')\n", (3223, 3238), False, 'from app.logs import fastapi_logger\n'), ((851, 883), 'sqlmodel.select', 'select', (['models.UsersLoginAttempt'], {}), '(models.UsersLoginAttempt)\n', (857, 883), False, 'from sqlmodel import select\n'), ((2075, 2107), 'sqlmodel.select', 'select', (['models.UsersLoginAttempt'], {}), '(models.UsersLoginAttempt)\n', (2081, 2107), False, 'from sqlmodel import select\n'), ((2754, 2786), 'sqlmodel.select', 'select', (['models.UsersLoginAttempt'], {}), '(models.UsersLoginAttempt)\n', (2760, 2786), False, 'from sqlmodel import select\n')]