from sqlalchemy import Column, Integer, Unicode, Float, Enum, ForeignKey
from sqlalchemy.orm import relationship

from helix.constants.anchor_type import AnchorType
from helix.constants.module_type import ModuleType
from helix.constants.system_type import SystemType
from helix.models.sql.inverter_brands import InverterBrand
from helix.models.sql.power_monitors import PowerMonitor
from helix.models.sql.power_stations import PowerStation
from helix.models.sql.shared_sql_base import Base
from helix.models.sql.standalone_inverters import StandaloneInverter


class Site(Base):
    __tablename__ = 'sites'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    project_name = Column(Unicode, nullable=False)
    building_height = Column(Float, nullable=False)
    building_width = Column(Float, nullable=False)
    building_length = Column(Float, nullable=False)
    parapet_height = Column(Float, nullable=False)
    wind_speed = Column(Integer, nullable=False)
    exposure_category = Column(Unicode, nullable=False)
    exposure_transition_distance = Column(Integer)
    ballast_block_weight = Column(Integer, nullable=False)
    max_psf = Column(Float, nullable=False)
    system_type = Column(Enum(SystemType.singleTilt.value, SystemType.dualTilt.value, name='SystemType'), nullable=False)
    module_type = Column(Enum(ModuleType.Cell96.value, ModuleType.Cell128.value, ModuleType.PSeries.value, name='ModuleType'), nullable=False)
    anchor_type = Column(Enum(AnchorType.OMG_PowerGrip.value, AnchorType.OMG_PowerGrip_Plus.value, AnchorType.EcoFasten.value, name='AnchorType'), nullable=False)
    spectral_response = Column(Float, nullable=False)
    seismic_importance_factor = Column(Float, nullable=False)
    cad_file = Column(Unicode)
    cad_file_name = Column(Unicode)
    dxf_file = Column(Unicode)
    dxf_file_name = Column(Unicode)

    inverter_brands = relationship(InverterBrand.__name__, backref="site", cascade="save-update, merge, delete")
    power_stations = relationship(PowerStation.__name__, backref="site", cascade="save-update, merge, delete")
    standalone_inverters = relationship(StandaloneInverter.__name__, backref="site", cascade="save-update, merge, delete")
    power_monitors = relationship(PowerMonitor.__name__, backref="site", cascade="save-update, merge, delete")
