from sqlalchemy import (
    JSON,
    UUID,
    Column,
    DateTime,
    Float,
    ForeignKey,
    Integer,
    Numeric,
    String,
)
from sqlalchemy.orm import relationship

from src.database.core import Base
from src.models import DefaultMixin
from sqlalchemy.dialects.postgresql import ARRAY


class AerosEquipment(Base, DefaultMixin):
    __tablename__ = "rbd_ms_aeros_equipment"

    # Basic equipment information
    node_name = Column(String, nullable=True)
    location_tag = Column(String, nullable=True)
    # max_flowrate = Column(Numeric, nullable=True)
    # design_flowrate = Column(Numeric, nullable=True)
    # flowrate_unit = Column(String, nullable=True)

    # # Release discharge parameters
    # rel_dis_type = Column(String, nullable=True)
    # rel_dis_p1 = Column(Numeric, nullable=True)
    # rel_dis_p2 = Column(Numeric, nullable=True)
    # rel_dis_p3 = Column(Numeric, nullable=True)
    # rel_dis_unit_code = Column(String, nullable=True)

    # # CM discharge parameters
    # cm_dis_type = Column(String, nullable=True)
    # cm_dis_p1 = Column(Numeric, nullable=True)
    # cm_dis_p2 = Column(Numeric, nullable=True)
    # cm_dis_p3 = Column(Numeric, nullable=True)
    # cm_dis_unit_code = Column(String, nullable=True)

    # # IP discharge parameters
    # ip_dis_type = Column(String, nullable=True)
    # ip_dis_p1 = Column(Numeric, nullable=True)
    # ip_dis_p2 = Column(Numeric, nullable=True)
    # ip_dis_p3 = Column(Numeric, nullable=True)
    # ip_dis_unit_code = Column(String, nullable=True)

    # # PM discharge parameters
    # pm_dis_type = Column(String, nullable=True)
    # pm_dis_p1 = Column(Numeric, nullable=True)
    # pm_dis_p2 = Column(Numeric, nullable=True)
    # pm_dis_p3 = Column(Numeric, nullable=True)
    # pm_dis_unit_code = Column(String, nullable=True)

    # # OH discharge parameters
    # oh_dis_type = Column(String, nullable=True)
    # oh_dis_p1 = Column(Numeric, nullable=True)
    # oh_dis_p2 = Column(Numeric, nullable=True)
    # oh_dis_p3 = Column(Numeric, nullable=True)
    # oh_dis_unit_code = Column(String, nullable=True)

    aeros_equipment_details = relationship(
        "AerosEquipmentDetail", back_populates="aeros_equipment", lazy="raise"
    )

    master_equipment = relationship(
        "MasterEquipment",
        lazy="raise",
        primaryjoin="and_(AerosEquipment.location_tag == foreign(MasterEquipment.location_tag))",
        uselist=False,  # Add this if it's a one-to-one relationship
    )

    aeros_node = relationship(
        "AerosNode",
        lazy="joined",
        primaryjoin="and_(AerosEquipment.node_name == foreign(AerosNode.node_name))",
        uselist=False,
    )

    custom_parameters = relationship(
        "AerosEquipmentCustomParameterData",
        lazy="raise",
        primaryjoin="and_(AerosEquipment.node_name == foreign(AerosEquipmentCustomParameterData.location_tag))",
        uselist=True,
    )


class AerosEquipmentDetail(Base, DefaultMixin):

    __tablename__ = "rbd_ms_aeros_equipment_detail"

    aeros_equipment_id = Column(
        UUID(as_uuid=True), ForeignKey("rbd_ms_aeros_equipment.id"), nullable=False
    )
    location_tag = Column(String, nullable=True)

    aeros_equipment = relationship(
        "AerosEquipment", back_populates="aeros_equipment_details", lazy="raise"
    )


class MasterEquipment(Base, DefaultMixin):
    __tablename__ = "ms_equipment_master"

    location_tag = Column(String, nullable=True)
    name = Column(String, nullable=True)


class AerosEquipmentCustomParameterData(Base, DefaultMixin):
    __tablename__ = 'rbd_ms_equipment_custom_input'

    location_tag = Column(String(100), nullable=False)
    level = Column(String(20), nullable=False)
    failure_rates = Column(ARRAY(Numeric), nullable=True)
    mttr = Column(Numeric, nullable=True)


class AerosEquipmentGroup(Base, DefaultMixin):
    __tablename__ = "rbd_ms_aeros_group_equipment"

    group_name = Column(String(100), nullable=False)

class ReliabilityPredictNonRepairable(Base, DefaultMixin):
    __tablename__ = "rp_non_repairable_results"

    location_tag = Column(String(100), nullable=False)
