from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from typing import Optional, List
from utils.utils import get_db
from . import handler
from .schema import UserCreate, UserUpdate, UserResponse, UserLogin, UserLoginResponse, UserProfileResponse, UserRole


router = APIRouter(prefix="/users", tags=["User Management"])


@router.post(
    "/register",
    response_model=UserResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Register New User",
    description="Creates a new user account with email validation and password hashing. Returns 409 if the email already exists. Password must be at least 8 characters with uppercase, lowercase, and number.",
)
def register_user_route(data: UserCreate, db: Session = Depends(get_db)):
    return handler.register_user(db, data)


@router.post(
    "/login",
    response_model=UserLoginResponse,
    summary="User Login",
    description="Authenticates user credentials and returns a JWT access token valid for 30 minutes. Returns 400 if credentials are invalid or account is inactive.",
)
def login_user_route(data: UserLogin, db: Session = Depends(get_db)):
    return handler.login_user(db, data)


@router.get(
    "/{id}/profile",
    response_model=UserProfileResponse,
    summary="Get User Profile",
    description="Retrieves the profile information for a specific user by ID. Returns 404 if the user does not exist.",
)
def get_user_profile_route(id: str, db: Session = Depends(get_db)):
    return handler.get_user_profile(db, id)


@router.put(
    "/{id}/profile",
    response_model=UserResponse,
    summary="Update User Profile",
    description="Updates user profile information including email, name, phone number, and password. Returns 404 if user not found, 409 if new email already exists. Only provided fields are updated.",
)
def update_user_profile_route(id: str, data: UserUpdate, db: Session = Depends(get_db)):
    return handler.update_user_profile(db, id, data)


@router.get(
    "/",
    response_model=List[UserResponse],
    summary="List Users",
    description="Returns a paginated list of users. Supports optional filtering by role and active status. Results are ordered by creation date descending.",
)
def list_users_route(
    limit: int = Query(20, ge=1, le=100),
    offset: int = Query(0, ge=0),
    role: Optional[UserRole] = Query(None),
    is_active: Optional[bool] = Query(None),
    db: Session = Depends(get_db),
):
    role_value = role.value if role else None
    return handler.list_users(db, limit, offset, role_value, is_active)


@router.get(
    "/{id}",
    response_model=UserResponse,
    summary="Get User By ID",
    description="Retrieves detailed information for a specific user by ID. Returns 404 if the user does not exist.",
)
def get_user_by_id_route(id: str, db: Session = Depends(get_db)):
    return handler.get_user_by_id(db, id)


@router.delete(
    "/{id}",
    status_code=status.HTTP_200_OK,
    summary="Delete User",
    description="Deletes a user account and all associated data including reservations, bookings, payments, and cancellations. Returns 404 if user not found.",
)
def delete_user_route(id: str, db: Session = Depends(get_db)):
    return handler.delete_user(db, id)