from sqlalchemy import Column, String, ForeignKey, Float, Numeric, ForeignKeyConstraint, Integer

from xplordb.datamodel.survey import Survey

from xplordb.sqlalchemy.base import Base


class XplordbSurveyTable(Survey, Base):
    """
    Define Xplordb columns for Survey definition
    """
    __tablename__ = "surv"

    data_set = Column("data_set", Integer, ForeignKey('ref.data_sets.id'), primary_key=True)
    hole_id = Column("hole_id", String, primary_key=True)
    loaded_by = Column("loaded_by", Integer,  ForeignKey('ref.person.id'))

    # Column defined as Numeric so they can be used as primary keys
    # Value in Survey class defined as float : not used as decimal
    depth = Column("depth_m", Numeric(asdecimal=False), primary_key=True)
    dip = Column("dip", Float)
    azimuth = Column("azimuth_grid", Float)

    __table_args__ = (
        ForeignKeyConstraint(
            ["hole_id", "data_set"],
            ["dh.collar.hole_id", "dh.collar.data_set"],
            ondelete="CASCADE"
        ),
        {"schema": "dh"}
    )