from sqlalchemy import Column, String, ForeignKey, Integer
from sqlalchemy.orm import relationship

from xplordb.datamodel.dataset import Dataset
from xplordb.sqlalchemy.base import Base


class XplordbDatasetTable(Dataset, Base):
    """
    Define spatialite columns for Dataset definition
    """
    __tablename__ = "data_sets"
    __table_args__ = {"schema": 'ref'}

    id = Column("id", Integer, primary_key = True)
    name = Column('name', String)
    person = Column('person', Integer, ForeignKey('ref.person.id'))

    collars = relationship("XplordbCollarTable")
    surveys = relationship("XplordbSurveyTable")
    
    person_rel = relationship("XplordbPersonTable", back_populates="datasets")


    def to_dataset(self) -> Dataset:

        person = self.person_rel
        return Dataset(name = self.name, person=person.to_person())