from fastapi import APIRouter, Depends, Query, UploadFile, File
from typing import Optional, List
from auth.models import TokenPayload
from auth.dependencies import require_permission
from users.models import (
    UserCreate, UserUpdate, UserInDB, UserListResponse,
    SetPasswordRequest, ResetPasswordRequest,
)
from users import service

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


@router.get("", response_model=UserListResponse)
async def list_users(
    search: Optional[str] = Query(None),
    status: Optional[str] = Query(None),
    _user: TokenPayload = Depends(require_permission("app_users", "view")),
):
    users = service.list_users(search=search, status=status)
    return UserListResponse(users=users, total=len(users))


@router.get("/{user_id}", response_model=UserInDB)
async def get_user(
    user_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "view")),
):
    return service.get_user(user_id)


@router.post("", response_model=UserInDB, status_code=201)
async def create_user(
    body: UserCreate,
    _user: TokenPayload = Depends(require_permission("app_users", "add")),
):
    return service.create_user(body)


@router.put("/{user_id}", response_model=UserInDB)
async def update_user(
    user_id: str,
    body: UserUpdate,
    _user: TokenPayload = Depends(require_permission("app_users", "edit")),
):
    return service.update_user(user_id, body)


@router.delete("/{user_id}", status_code=204)
async def delete_user(
    user_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "delete")),
):
    service.delete_user(user_id)


@router.post("/{user_id}/block", response_model=UserInDB)
async def block_user(
    user_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "edit")),
):
    return service.block_user(user_id)


@router.post("/{user_id}/unblock", response_model=UserInDB)
async def unblock_user(
    user_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "edit")),
):
    return service.unblock_user(user_id)


@router.get("/{user_id}/devices", response_model=List[dict])
async def get_user_devices(
    user_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "view")),
):
    return service.get_user_devices(user_id)


@router.post("/{user_id}/devices/{device_id}", response_model=UserInDB)
async def assign_device(
    user_id: str,
    device_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "edit")),
):
    return service.assign_device(user_id, device_id)


@router.delete("/{user_id}/devices/{device_id}", response_model=UserInDB)
async def unassign_device(
    user_id: str,
    device_id: str,
    _user: TokenPayload = Depends(require_permission("app_users", "edit")),
):
    return service.unassign_device(user_id, device_id)


@router.post("/{user_id}/set-password", status_code=204)
async def set_password(
    user_id: str,
    body: SetPasswordRequest,
    _user: TokenPayload = Depends(require_permission("app_users", "full_edit")),
):
    """Set a new password for the user via Firebase Auth (requires uid on the user doc)."""
    service.set_password(user_id, body.password)


@router.post("/{user_id}/reset-password", status_code=204)
async def reset_password(
    user_id: str,
    body: ResetPasswordRequest,
    _user: TokenPayload = Depends(require_permission("app_users", "full_edit")),
):
    """Reset a user's password to the supplied value (default: Bell1234!)."""
    service.set_password(user_id, body.new_password)


@router.post("/{user_id}/photo")
async def upload_photo(
    user_id: str,
    file: UploadFile = File(...),
    _user: TokenPayload = Depends(require_permission("app_users", "edit")),
):
    contents = await file.read()
    content_type = file.content_type or "image/jpeg"
    url = service.upload_photo(user_id, contents, file.filename, content_type)
    return {"photo_url": url}
