from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import Dict, Any

from app.core import get_db, get_current_active_user, get_current_admin_user
from app import services
from app.schemas.user_schemas import UserUpdate, AddressCreate, AddressUpdate
from app.models.user_models import User as UserModel

# Роутер для пользователей
user_router = APIRouter(prefix="/users", tags=["Пользователи"])

@user_router.get("/me", response_model=Dict[str, Any])
async def read_users_me(current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.get_user_profile(db, current_user.id)


@user_router.put("/me", response_model=Dict[str, Any])
async def update_user_me(user_data: UserUpdate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.update_user_profile(db, current_user.id, user_data)


@user_router.post("/me/addresses", response_model=Dict[str, Any])
async def create_user_address(address: AddressCreate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.add_user_address(db, current_user.id, address)


@user_router.put("/me/addresses/{address_id}", response_model=Dict[str, Any])
async def update_user_address_endpoint(address_id: int, address: AddressUpdate, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.update_user_address(db, current_user.id, address_id, address)


@user_router.delete("/me/addresses/{address_id}", response_model=Dict[str, Any])
async def delete_user_address_endpoint(address_id: int, current_user: UserModel = Depends(get_current_active_user), db: Session = Depends(get_db)):
    return services.delete_user_address(db, current_user.id, address_id)


@user_router.get("/{user_id}", response_model=Dict[str, Any])
async def read_user(user_id: int, current_user: UserModel = Depends(get_current_admin_user), db: Session = Depends(get_db)):
    return services.get_user_profile(db, user_id) 