from fastapi import APIRouter, Depends, Form
from typing_extensions import Annotated

from core.config import get_db
from core.security.authentication import hash_password, get_user
from schema import GenericResponse
from schema.user import ForgotPasswordRequest, ResetPasswordRequest, UserCreateSchema, UserLoginSchema, ProfileSchema, ProfileImage, ChangePasswordRequest
from services.participant import ParticipantService
from services.user import UserService
from services.parent import ParentService
from core.email.config import SendWelcomeEmail

user_router = APIRouter(prefix="/user")


@user_router.post("/registration", response_model=GenericResponse)
async def user_registration(request: UserCreateSchema):
    if request.user_type == "parent":
        ParentService().create(request)
    if request.user_type == 'participant':
        ParticipantService().register_new_participant(request)
    if request.user_type == "guest":
        UserService().create_user(request)
    recipients = [
        {
            'email': request.email,
            'name': request.first_name
        }
    ]
    email = SendWelcomeEmail(to=recipients, subject='Welcome')
    email.send_email()
    return {'message': "Account created successfully"}


@user_router.post("/login", response_model=GenericResponse)
async def user_login(request: UserLoginSchema, service: UserService = Depends(UserService)):
    return {'message': "Account fetched successfully", "data": service.validate_user_for_login(request)}


# @user_router.post("/profile", response_model=GenericResponse)
# async def create_update_profile(request: ProfileSchema, user=Depends(get_user), service=Depends(UserService)):
#     service.update_profile(request, user)
#     return {"message": "Profile updated successfully."}


# @user_router.get("/profile", response_model=GenericResponse)
# async def fetch_profile(user=Depends(get_user), service: UserService = Depends(UserService)):
#     return {"message": "Profile populated successfully.", "data": service.fetch_profile(user)}


@user_router.post("/image", response_model=GenericResponse)
async def upload_profile_image(request: Annotated[ProfileImage, Form()], user=Depends(get_user),
                               service: UserService = Depends(UserService)):

    await service.upload_profile_image(request, user)

    return {"message": "Profile image uploaded successfully."}


@user_router.get("/parent/all", response_model=GenericResponse)
async def get_all_parent_user(user=Depends(get_user), service: UserService = Depends(UserService)):
    data=ParentService().fetch_all_parents()
    return {"message": "Parent data populated successfully", "data": data}


@user_router.get("/participant/all", response_model=GenericResponse)
async def get_all_participant_user(user=Depends(get_user), service: ParticipantService = Depends(ParticipantService)):
    data = service.get_all_participant()
    return {"message": "Participant data populated successfully", "data": data}


@user_router.post("/forgot-password", response_model=GenericResponse)
async def forgot_password(request: ForgotPasswordRequest, service: UserService = Depends(UserService)):
    service.send_password_reset_email(request)
    return {"message": "Password reset instructions have been sent to your email."}

@user_router.post("/reset-password", response_model=GenericResponse)
async def reset_password(request: ResetPasswordRequest, service: UserService = Depends(UserService)):
    service.reset_password(request)
    return {"message": "Password has been reset successfully."}


@user_router.post("/change-password", response_model=GenericResponse)
async def change_password(
    request: ChangePasswordRequest,
    user=Depends(get_user),
    service: UserService = Depends(UserService)
):
    await service.change_password(user, request)
    return {"message": "Password changed successfully."}


@user_router.post("/{user_id}/toggle-active", response_model=GenericResponse)
async def toggle_user_active(
    user_id: int,
    user_type: str,  # This helps identify the model
    service: UserService = Depends(UserService)
):
    new_status = service.toggle_user_active(user_id, user_type)
    status = "activated" if new_status else "deactivated"
    return {"message": f"User {status} successfully"}


