from typing import Optional

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.database.session import get_db
from app.domain.entities.change_password import ChangePasswordEntity
from app.domain.entities.user import UserEntity
from app.infrastructure.dependencies import get_current_user
from app.infrastructure.users_service import UsersService

router = APIRouter()


@router.get(
    '/my-data/',
    response_model=Optional[UserEntity],
    summary='Returns current authenticated user data',
    description='Returns current authenticated user data',
)
async def get_authenticated_user_data(
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    users_service = UsersService(db)
    return await users_service.get_by_id(user.id)


@router.get(
    '/change-password/',
    response_model=Optional[UserEntity],
    summary='Change password for user',
    description='Changes password for user',
)
async def get_authenticated_user_data(
        data: ChangePasswordEntity,
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    users_service = UsersService(db)
    return await users_service.get_by_id(data.user_id, data.new_password, user.id)
