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


class PowerStation(Base):
    __tablename__ = 'power_stations'
    id = Column(Integer, primary_key=True)
    site_id = Column(Integer, ForeignKey('sites.id'))
    quantity = Column(Integer, nullable=False)
    ac_run_length = Column(Integer, nullable=False)
    description = Column(Unicode, nullable=False)
    inverters = relationship(Inverter.__name__, backref="power_stations", cascade="save-update, merge, delete")

    def to_json(self):
        return {
            'inverter_quantity': len(self.inverters),
            'power_station_quantity': self.quantity,
            'power_station_description': self.description,
            'power_station_id': self.id,
            'ac_run_length': self.ac_run_length,
            'inverters': [inverter.to_json() for inverter in self.inverters]
        }
