"""create records tables

Revision ID: a7522972878d
Revises: 7e5a8cabd3a4
Create Date: 2023-05-13 04:46:57.958926

"""
from alembic import op
from sqlalchemy import Column, ForeignKey, Integer, SmallInteger, DateTime, Enum, Numeric
from sqlalchemy.sql.functions import now

from backend.models.records import AvpuScore, RespirationScore


# revision identifiers, used by Alembic.
revision = 'a7522972878d'
down_revision = '7e5a8cabd3a4'
branch_labels = None
depends_on = down_revision


def upgrade() -> None:
    op.create_table(
        'heart_rate_records',
        Column('id', Integer, primary_key=True, autoincrement=True, index=True),
        Column('measured', DateTime(timezone=True), nullable=False, index=True),
        Column('value', SmallInteger, nullable=False),
        Column('device_id', Integer, ForeignKey('devices.id', ondelete="CASCADE"), nullable=False, index=True),
    )

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

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

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

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

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


def downgrade() -> None:
    op.drop_table('heart_rate_records')
    op.drop_table('avpu_score_records')
    op.drop_table('body_temperature_records')
    op.drop_table('blood_pressure_records')
    op.drop_table('blood_oxygen_records')
    op.drop_table('respiration_score_records')
