from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from helix.models.sql.shared_sql_base import Base


class PowerMonitor(Base):
    __tablename__ = 'power_monitors'
    id = Column(Integer, primary_key=True)
    site_id = Column(Integer, ForeignKey('sites.id'))
    power_station_id = Column(Integer, ForeignKey('power_stations.id'))
    power_station = relationship("PowerStation")

    def to_json(self):
        if self.power_station:
            power_source = (self.power_station.description, self.power_station.id)
        else:
            power_source = ('Switch Gear/External', None)
        return {
            'monitor_id': self.id,
            'power_source': power_source
        }
