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

from xplordb.datamodel.person import Person
from xplordb.datamodel.company import Company

from xplordb.sqlalchemy.base import Base


class XplordbPersonTable(Person, Base):
    """
    Define Xplordb columns for Person definition
    """
    __tablename__ = "person"
    __table_args__ = {"schema": 'ref'}

    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    company = Column("company", String, ForeignKey('ref.company.name'))
    
    company_rel = relationship("XplordbCompanyTable", back_populates="persons")
    datasets = relationship("XplordbDatasetTable", back_populates="person_rel")

    def to_person(self) -> Person:

        return Person(name=self.name,
                      company=Company(name=self.company))