from typing import Optional

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

from app.database.session import get_db
from app.domain.entities.scheduled_appointment import ScheduledAppointmentEntity
from app.infrastructure.dependencies import get_current_user
from app.infrastructure.scheduled_appointments_service import ScheduledAppointmentsService

router = APIRouter()


@router.get(
    "/",
    response_model=list[ScheduledAppointmentEntity],
    summary="Get all scheduled appointments",
    description="Returns a list of all scheduled appointments",
)
async def get_all_scheduled_appointments(
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    scheduled_appointments_service = ScheduledAppointmentsService(db)
    return await scheduled_appointments_service.get_all_scheduled_appointments()


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


@router.get(
    "/patient/{patient_id}/",
    response_model=ScheduledAppointmentEntity,
    summary="Get all scheduled appointments for patient",
    description="Returns a list of scheduled 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 = ScheduledAppointmentsService(db)
    return await appointments_service.get_scheduled_appointments_by_patient_id(patient_id)


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


@router.post(
    "/{appointment_id}/cancel/",
    response_model=Optional[ScheduledAppointmentEntity],
    summary="Cancel scheduled appointment",
    description="Cancel scheduled appointment",
)
async def cancel_appointment(
        appointment_id: int,
        db: AsyncSession = Depends(get_db),
        user=Depends(get_current_user),
):
    appointment_service = ScheduledAppointmentsService(db)
    return await appointment_service.cancel_scheduled_appointment(appointment_id, user.id)


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