"""This module contains endpoints for operations related to vitals records."""

from datetime import datetime
from datetime import datetime

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from backend.database.engine import get_db
from backend.schemas import records as recordschema
from backend.crud import records as recordcrud
from backend.exceptions import NotFoundException


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

tag_metadata = {
    "name": "records",
    "description": "Operations related to vitals records."
}


@router.post("/{device_id}/heart-rate/", response_model=recordschema.HeartRateRecord)
def create_heart_rate_record(record: recordschema.HeartRateRecordCreate, device_id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.create_heart_rate_record(db, record, device_id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.get("/{device_id}/heart-rate/", response_model=list[recordschema.HeartRateRecord])
def read_heart_rate_records(
    device_id: int, db: Session = Depends(get_db),
    since: datetime | None = None, until: datetime | None = None,
    skip: int = 0, limit: int = 100,
    ):
    try:
        return recordcrud.read_heart_rate_records_by_device(db, device_id, since, until, skip, limit)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.delete("/{device_id}/heart-rate/{record_id}", response_model=recordschema.HeartRateRecord)
def delete_heart_rate_record(id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.delete_heart_rate_record(db, id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.post("/{device_id}/avpu-score/", response_model=recordschema.AvpuScoreRecord)
def create_avpu_score_record(record: recordschema.AvpuScoreRecordCreate, device_id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.create_avpu_score_record(db, record, device_id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.get("/{device_id}/avpu-score/", response_model=list[recordschema.AvpuScoreRecord])
def read_avpu_score_records(
    device_id: int, db: Session = Depends(get_db),
    since: datetime | None = None, until: datetime | None = None,
    skip: int = 0, limit: int = 100,
    ):
    try:
        return recordcrud.read_avpu_score_records_by_device(db, device_id, since, until, skip, limit)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.delete("/{device_id}/avpu-score/{record_id}", response_model=recordschema.AvpuScoreRecord)
def delete_avpu_score_record(id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.delete_avpu_score_record(db, id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.post("/{device_id}/body-temperature/", response_model=recordschema.BodyTemperatureRecord)
def create_body_temperature_record(record: recordschema.BodyTemperatureRecordCreate, device_id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.create_body_temperature_record(db, record, device_id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.get("/{device_id}/body-temperature/", response_model=list[recordschema.BodyTemperatureRecord])
def read_body_temperature_records(
    device_id: int, db: Session = Depends(get_db),
    since: datetime | None = None, until: datetime | None = None,
    skip: int = 0, limit: int = 100,
    ):
    try:
        return recordcrud.read_body_temperature_records_by_device(db, device_id, since, until, skip, limit)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.delete("/{device_id}/body-temperature/{record_id}", response_model=recordschema.BodyTemperatureRecord)
def delete_body_temperature_record(id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.delete_body_temperature_record(db, id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.post("/{device_id}/blood-pressure/", response_model=recordschema.BloodPressureRecord)
def create_blood_pressure_record(record: recordschema.BloodPressureRecordCreate, device_id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.create_blood_pressure_record(db, record, device_id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.get("/{device_id}/blood-pressure/", response_model=list[recordschema.BloodPressureRecord])
def read_blood_pressure_records(
    device_id: int, db: Session = Depends(get_db),
    since: datetime | None = None, until: datetime | None = None,
    skip: int = 0, limit: int = 100,
    ):
    try:
        return recordcrud.read_blood_pressure_records_by_device(db, device_id, since, until, skip, limit)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.delete("/{device_id}/blood-pressure/{record_id}", response_model=recordschema.BloodPressureRecord)
def delete_blood_pressure_record(id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.delete_blood_pressure_record(db, id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.post("/{device_id}/blood-oxygen/", response_model=recordschema.BloodOxygenRecord)
def create_blood_oxygen_record(record: recordschema.BloodOxygenRecordCreate, device_id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.create_blood_oxygen_record(db, record, device_id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.get("/{device_id}/blood-oxygen/", response_model=list[recordschema.BloodOxygenRecord])
def read_blood_oxygen_records(
    device_id: int, db: Session = Depends(get_db),
    since: datetime | None = None, until: datetime | None = None,
    skip: int = 0, limit: int = 100,
    ):
    try:
        return recordcrud.read_blood_oxygen_records_by_device(db, device_id, since, until, skip, limit)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.delete("/{device_id}/blood-oxygen/{record_id}", response_model=recordschema.BloodOxygenRecord)
def delete_blood_oxygen_record(id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.delete_blood_oxygen_record(db, id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.post("/{device_id}/respiration-score/", response_model=recordschema.RespirationScoreRecord)
def create_respiration_score_record(record: recordschema.RespirationScoreRecordCreate, device_id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.create_respiration_score_record(db, record, device_id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.get("/{device_id}/respiration-score/", response_model=list[recordschema.RespirationScoreRecord])
def read_respiration_score_records(
    device_id: int, db: Session = Depends(get_db),
    since: datetime | None = None, until: datetime | None = None,
    skip: int = 0, limit: int = 100,
    ):
    try:
        return recordcrud.read_respiration_score_records_by_device(db, device_id, since, until, skip, limit)
    except NotFoundException as e:
        raise HTTPException(404, str(e))


@router.delete("/{device_id}/respiration-score/{record_id}", response_model=recordschema.RespirationScoreRecord)
def delete_respiration_score_record(id: int, db: Session = Depends(get_db)):
    try:
        return recordcrud.delete_respiration_score_record(db, id)
    except NotFoundException as e:
        raise HTTPException(404, str(e))
