from sqlalchemy.schema import UniqueConstraint
from sqlalchemy.orm import backref
from app import db

class Device(db.Model):
  __tablename__ = "device"
  id = db.Column(db.Integer, primary_key = True)
  device_id = db.Column(db.String(10), nullable = False, unique = True) 

class Reading(db.Model):
  __tablename__ = "reading"
  id = db.Column(db.Integer, primary_key = True)
  rdate= db.Column(db.DateTime, nullable = False)
  input_voltage_l1 = db.Column(db.Float, nullable = False)
  input_voltage_l2 = db.Column(db.Float, nullable = False)
  input_voltage_l3 = db.Column(db.Float, nullable = False)
  output_voltage = db.Column(db.Float, nullable = False)
  current_l1 = db.Column(db.Float, nullable = False)
  current_l2 = db.Column(db.Float, nullable = False)
  current_l3 = db.Column(db.Float, nullable = False)
  pf_l1 = db.Column(db.Float, nullable = False)
  pf_l2 = db.Column(db.Float, nullable = False)
  pf_l3 = db.Column(db.Float, nullable = False)
  power_l1 = db.Column(db.Float, nullable = False)
  power_l2 = db.Column(db.Float, nullable = False)
  power_l3 = db.Column(db.Float, nullable = False)
  total_power_kw = db.Column(db.Float, nullable = False)
  total_kwh = db.Column(db.Float, nullable = False)
  window2_consumption = db.Column(db.Float, nullable = False)
  today_saved = db.Column(db.Float, nullable = False)
  today_consumption = db.Column(db.Float, nullable = False)
  yesterday_saved = db.Column(db.Float, nullable = False)
  yesterday_consumption = db.Column(db.Float, nullable = False)
  weekly_saved = db.Column(db.Float, nullable = False)
  weekly_consumption = db.Column(db.Float, nullable = False)
  last_week_saved = db.Column(db.Float, nullable = False)
  last_week_consumption = db.Column(db.Float, nullable = False)
  monthly_saved = db.Column(db.Float, nullable = False)
  monthly_consumption = db.Column(db.Float, nullable = False)
  last_month_saved = db.Column(db.Float, nullable = False)
  last_month_consumption = db.Column(db.Float, nullable = False)
  yearly_saved = db.Column(db.Float, nullable = False)
  yearly_consumption = db.Column(db.Float, nullable = False)
  last_year_saved = db.Column(db.Float, nullable = False)
  last_year_consumption = db.Column(db.Float, nullable = False)
  over_load_indication = db.Column(db.Float, nullable = False)
  missing_phase_indication = db.Column(db.Float, nullable = False)
  over_temperature_indication = db.Column(db.Float, nullable = False)
  temperature_l1 = db.Column(db.Float, nullable = False)
  temperature_l2 = db.Column(db.Float, nullable = False)
  temperature_l3 = db.Column(db.Float, nullable = False)
  cpu_temperature = db.Column(db.Float, nullable = False)
  device_mode = db.Column(db.Float, nullable = False)
  reduction_step = db.Column(db.Float, nullable = False)
  software_resets = db.Column(db.Float, nullable = False)
  device_id = db.Column(db.Integer, db.ForeignKey("device.id"))
  readings_for_device = db.relationship("Device", backref = backref("readings", cascade = "all, delete-orphan", single_parent = True))
  __table_args__ = (UniqueConstraint('rdate', 'device_id', name='_date_device_uc'), )
