from fastapi import HTTPException, status

from app.v1.model.user_model import User as UserModel
from app.v1.schema import user_schema
from app.v1.service.auth_service import get_password_hash
from app.v1.service import registered_developers_service

from app.v1.schema.registered_developers_schema import RegisteredDeveloper
from app.v1.schema.validation_fk_schema import ValidationFk

def create_user(user: user_schema.UserRegister):

    get_user = UserModel.filter((UserModel.email == user.email) | (UserModel.username == user.username)).first()
    if get_user:
        msg = "Email already registered"
        if get_user.username == user.username:
            msg = "Username already registered"
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=msg,
        )
    
    # developer
    db_user = UserModel(
        email = user.email,
        username = user.username,
        password = get_password_hash(user.password),
        is_admin = False,
        is_manager = False,
        is_developer = True,
    )

    db_user.save()

    return user_schema.User(
        id=db_user.id,
        email=db_user.email,
        username= db_user.username,
        is_admin= db_user.is_admin,
        is_manager=db_user.is_manager,
        is_developer=db_user.is_developer,
    )

def admin_create_user(user: user_schema.AdminRegisterUsers):

    get_user = UserModel.filter((UserModel.email == user.email) | (UserModel.username == user.username)).first()
    if get_user:
        msg = "Email already registered"
        if get_user.username == user.username:
            msg = "Username already registered"
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=msg,
        )
    
    # developer
    db_user = UserModel(
        email = user.email,
        username = user.username,
        password = get_password_hash(user.password),
        is_admin = user.is_admin,
        is_manager = user.is_manager,
        is_developer = user.is_developer,
    )

    db_user.save()

    return user_schema.User(
        id=db_user.id,
        email=db_user.email,
        username= db_user.username,
        is_admin= db_user.is_admin,
        is_manager=db_user.is_manager,
        is_developer=db_user.is_developer,
    )

def update_user(user_id:int,update_user:user_schema.AdminRegisterUsers):

    user=UserModel.filter(UserModel.id == user_id).first()

    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="user not found"
        )
    
    get_same_email = update_user.email == user.email
    get_same_username=update_user.username == user.username

    if not get_same_username:

        get_user = UserModel.filter(UserModel.username == update_user.username).first()

        if get_user:

            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Username already registered in another user"
            )
        
    if not get_same_email:

        get_user = UserModel.filter(UserModel.email == update_user.email).first()

        if get_user:

            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Email already registered in another user"
            )
        
    # developer
    db_user = UserModel(
        id=user_id,
        email = update_user.email,
        username = update_user.username,
        password = get_password_hash(update_user.password),
        is_admin = update_user.is_admin,
        is_manager = update_user.is_manager,
        is_developer = update_user.is_developer,
    )

    db_user.save()

    return user_schema.User(
        id=db_user.id,
        email=db_user.email,
        username= db_user.username,
        is_admin= db_user.is_admin,
        is_manager=db_user.is_manager,
        is_developer=db_user.is_developer,
    )


def get_users():
    users = UserModel.filter((UserModel.username != ''))
    list_users = []
    for user in users:
        list_users.append(
            user_schema.User(
                id=user.id,
                email=user.email,
                username= user.username,
                is_admin= user.is_admin,
                is_manager=user.is_manager,
                is_developer=user.is_developer,
            )
        )
    return list_users

def get_user(user_id: int):
    user = UserModel.filter(UserModel.id == user_id).first()

    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="user not found"
        )
    
    return user_schema.User(
        id=user.id,
        email=user.email,
        username= user.username,
        is_admin= user.is_admin,
        is_manager=user.is_manager,
        is_developer=user.is_developer,
    )

def check_user_as_fk(user_id:int):
    user = UserModel.filter(UserModel.id == user_id).first()

    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="user not found"
        )

    registered_developer = registered_developers_service.get_registered_developer_by_user_id(user_id)
    #print(registered_developer)

    if registered_developer:
        return ValidationFk(
            table_name="registered_developer",
            list_registers=registered_developer
        )
    else:
        return ValidationFk(
            table_name="registered_developer",
            list_registers=False,
        )

def update_role_user(user_id: int,roles: user_schema.UpdateRoles):
    user = UserModel.filter(UserModel.id == user_id).first()

    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="user not found"
        )
    
    user.is_manager=roles.is_manager
    user.is_admin=roles.is_admin
    user.is_developer=roles.is_developer
    user.save()

    return user_schema.User(
        id=user.id,
        email=user.email,
        username= user.username,
        is_admin= user.is_admin,
        is_manager=user.is_manager,
        is_developer=user.is_developer,
    )

def update_password(user_id: int,password_update:user_schema.UpdatePassword):
    
    user = UserModel.filter(UserModel.id == user_id).first()

    if not user:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="user not found"
        )
    
    user.password=get_password_hash(password_update.password)
    user.save()

    return user_schema.User(
        id=user.id,
        email=user.email,
        username= user.username,
        is_admin= user.is_admin,
        is_manager=user.is_manager,
        is_developer=user.is_developer,
    )


def delete_user(user_id: int,current_user_id:int):

    if user_id == current_user_id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="cannot delete current user"
        )
    else:
        user = UserModel.filter(UserModel.id == user_id).first()

        if not user:
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="user not found"
            )

        ValidationFk = check_user_as_fk(user_id)
        #print(ValidationFk)

        if ValidationFk:

            if isinstance(ValidationFk.list_registers,RegisteredDeveloper):
                registered_developer = ValidationFk.list_registers
                try:
                    registered_developers_service.delete_registered_developer(registered_developer.id)
                except:
                    print(f"registered developer {registered_developer} was not deleted")

        user.delete_instance()