"""This module defines the SQL data model for vital parameter records."""

import enum

from sqlalchemy import Column, ForeignKey, DateTime, SmallInteger, Integer, Enum, Numeric
from sqlalchemy.orm import relationship

from backend.database.engine import Base


class HeartRateRecord(Base):
    """Model for the heart rate records table. Measured in beats per minute (bpm)."""

    __tablename__ = "heart_rate_records"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    measured = Column('measured', DateTime(timezone=True), nullable=False, index=True)
    value = Column('value', SmallInteger, nullable=False)
    device_id = Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True)

    device = relationship("Device", back_populates="heart_rate_records", uselist=False)


class AvpuScore(enum.Enum):
    alert = 'a'
    voice = 'v'
    pain = 'p'
    unresponsive = 'u'


class AvpuScoreRecord(Base):
    """Model for the avpu score records table. Measured as a discrete classification."""

    __tablename__ = "avpu_score_records"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    measured = Column('measured', DateTime(timezone=True), nullable=False, index=True)
    value = Column('value', Enum(AvpuScore), nullable=False)
    device_id = Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True)

    device = relationship("Device", back_populates="avpu_score_records", uselist=False)


class BodyTemperatureRecord(Base):
    """Model for the body temperature records table. Measured in degrees Celsius.

    Two digits before and two digits after the decimal point: [-99.99, 99.99].
    """

    __tablename__ = "body_temperature_records"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    measured = Column('measured', DateTime(timezone=True), nullable=False, index=True)
    value = Column('value', Numeric(precision=4, scale=2, decimal_return_scale=2), nullable=False)
    device_id = Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True)

    device = relationship("Device", back_populates="body_temperature_records", uselist=False)


class BloodPressureRecord(Base):
    """Model for the blood pressure records table. Measured in (mmHg)."""

    __tablename__ = "blood_pressure_records"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    measured = Column('measured', DateTime(timezone=True), nullable=False, index=True)
    value_systolic = Column('value_systolic', SmallInteger, nullable=False)
    value_diastolic = Column('value_diastolic', SmallInteger, nullable=False)
    device_id = Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True)

    device = relationship("Device", back_populates="blood_pressure_records", uselist=False)


class BloodOxygenRecord(Base):
    """Model for the blood oxygen records table. Measured as a decimal percentage.

    Three digits before and two digits after the decimal point: [-999.99, 999.99].
    """

    __tablename__ = "blood_oxygen_records"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    measured = Column('measured', DateTime(timezone=True), nullable=False, index=True)
    value = Column('value', Numeric(precision=5, scale=2, decimal_return_scale=2), nullable=False)
    device_id = Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True)

    device = relationship("Device", back_populates="blood_oxygen_records", uselist=False)


class RespirationScore(enum.Enum):
    """Measures whether patient is experiencing shortness of breath, and its severity."""

    none = 0
    low = 1
    medium = 2
    high = 3


class RespirationScoreRecord(Base):
    """Model for the respiration score records table. Measured as a discrete classification."""

    __tablename__ = "respiration_score_records"

    id = Column('id', Integer, primary_key=True, autoincrement=True, index=True)
    measured = Column('measured', DateTime(timezone=True), nullable=False, index=True)
    value = Column('value', Enum(RespirationScore), nullable=False)
    device_id = Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True)

    device = relationship("Device", back_populates="respiration_score_records", uselist=False)
