from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.database.session import get_db
from app.domain.entities.appointment import AppointmentEntity
from app.infrastructure.appointments_service import AppointmentsService
from app.infrastructure.dependencies import get_current_user

router = APIRouter()


@router.get(
    "/",
    response_model=list[AppointmentEntity],
    summary="Get all appointments",
    description="Returns a list of all appointments",
)
async def get_all_appointments(
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    appointments_service = AppointmentsService(db)
    return await appointments_service.get_all_appointments()


@router.get(
    "/doctor/{doctor_id}/",
    response_model=AppointmentEntity,
    summary="Get all appointments for doctor",
    description="Returns a list of appointments for doctor",
)
async def get_all_appointments_by_doctor_id(
        doctor_id: int,
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    appointments_service = AppointmentsService(db)
    return await appointments_service.get_appointments_by_doctor_id(doctor_id)


@router.get(
    "/patient/{patient_id}/",
    response_model=list[AppointmentEntity],
    summary="Get all appointments for patient",
    description="Returns a list of appointments for patient",
)
async def get_all_appointments_by_patient_id(
        patient_id: int,
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    appointments_service = AppointmentsService(db)
    return await appointments_service.get_appointments_by_patient_id(patient_id)


@router.post(
    "/",
    response_model=AppointmentEntity,
    summary="Create appointment",
    description="Creates a new appointment",
)
async def create_appointment(
        appointment: AppointmentEntity,
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    appointment_service = AppointmentsService(db)
    return await appointment_service.create_appointment(appointment, user.id)


@router.put(
    "/{appointment_id}/",
    response_model=AppointmentEntity,
    summary="Update appointment",
    description="Updates an existing appointment",
)
async def update_appointment(
        appointment_id: int,
        appointment: AppointmentEntity,
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    appointment_service = AppointmentsService(db)
    return await appointment_service.update_appointment(appointment_id, appointment)
