from fastapi import APIRouter, HTTPException, status, Depends
from app.schemas import ProfileCreate, ProfileUpdate, ProfileResponse, DiscoverResponse
from app.services.profile_service import ProfileService
from app.auth import get_current_user

router = APIRouter(prefix="/profiles", tags=["profiles"])

@router.post("/", response_model=ProfileResponse)
def create_or_update_profile(
    profile_data: ProfileCreate,
    current_user: dict = Depends(get_current_user)
):
    """Create or update user profile"""
    try:
        return ProfileService.create_profile(current_user["user_id"], profile_data)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )

@router.get("/me", response_model=ProfileResponse)
def get_my_profile(current_user: dict = Depends(get_current_user)):
    """Get current user's profile"""
    profile = ProfileService.get_profile_by_user(current_user["user_id"])
    if not profile:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Profile not found"
        )
    return profile

@router.get("/{user_id}", response_model=ProfileResponse)
def get_profile(user_id: int, current_user: dict = Depends(get_current_user)):
    """Get profile by user ID"""
    profile = ProfileService.get_profile_by_user(user_id)
    if not profile:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="Profile not found"
        )
    return profile

@router.get("/discover/list", response_model=list)
def discover_profiles(current_user: dict = Depends(get_current_user)):
    """Get profiles to discover"""
    return ProfileService.discover_profiles(current_user["user_id"])
