from __future__ import annotations

from typing import Annotated
from uuid import UUID

from fastapi import APIRouter, Depends, status

from v1.friendships.dependencies import get_friendship_service
from v1.friendships.schemas import (
    FriendRequestAction,
    FriendRequestCreate,
    FriendRequestResponse,
    FriendResponse,
)
from v1.friendships.service import FriendshipService


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


@router.post(
    "/requests",
    response_model=FriendRequestResponse,
    status_code=status.HTTP_201_CREATED,
)
async def send_friend_request(
    payload: FriendRequestCreate,
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> FriendRequestResponse:
    return await service.send_request(payload)


@router.get("/requests/inbox", response_model=list[FriendRequestResponse])
async def get_inbox(
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> list[FriendRequestResponse]:
    return await service.get_inbox()


@router.get("/requests/outgoing", response_model=list[FriendRequestResponse])
async def get_outgoing_requests(
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> list[FriendRequestResponse]:
    return await service.get_outgoing_requests()


@router.post(
    "/requests/{friendship_id}/accept",
    response_model=FriendRequestResponse,
)
async def accept_friend_request(
    friendship_id: UUID,
    _: FriendRequestAction,
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> FriendRequestResponse:
    return await service.accept_request(friendship_id)


@router.post(
    "/requests/{friendship_id}/decline",
    response_model=FriendRequestResponse,
)
async def decline_friend_request(
    friendship_id: UUID,
    _: FriendRequestAction,
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> FriendRequestResponse:
    return await service.decline_request(friendship_id)


@router.delete(
    "/requests/{friendship_id}",
    status_code=status.HTTP_204_NO_CONTENT,
)
async def cancel_friend_request(
    friendship_id: UUID,
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> None:
    await service.cancel_request(friendship_id)


@router.get("", response_model=list[FriendResponse])
async def get_friends_list(
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> list[FriendResponse]:
    return await service.get_friends_list()


@router.delete(
    "/{friendship_id}",
    status_code=status.HTTP_204_NO_CONTENT,
)
async def remove_friend(
    friendship_id: UUID,
    service: Annotated[FriendshipService, Depends(get_friendship_service)],
) -> None:
    await service.remove_friend(friendship_id)
