from typing import List

from app.database import get_session
from app.models import Medication, MedicationUpdate
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import select

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


@router.post("", response_model=Medication)
async def create_medication(
    *, med: Medication, session: AsyncSession = Depends(get_session)
) -> Medication:
    medication = Medication.from_orm(med)
    session.add(medication)
    await session.commit()
    await session.refresh(medication)
    return medication


@router.get("/{medication_id}", response_model=Medication)
async def retrieve_medication(
    *, medication_id: str, session: AsyncSession = Depends(get_session)
) -> Medication:
    result = await session.execute(
        select(Medication).where(Medication.id == medication_id)
    )
    medication = result.scalar_one_or_none()
    if not medication:
        raise HTTPException(
            status_code=404, detail=f"Medication {medication_id} not found"
        )
    return medication


@router.patch("/{medication_id}", response_model=Medication)
async def update_medication(
    *,
    medication_id: str,
    patch: MedicationUpdate,
    session: AsyncSession = Depends(get_session),
) -> Medication:
    result = await session.execute(
        