import uuid

from sqlalchemy import Table
from sqlalchemy.dialects.postgresql import UUID

import Models
from app import db

interests_table = Table('user_interests', Models.Base.metadata,
                        db.Column('user_id', db.ForeignKey('user.id')),
                        db.Column('interest_id', db.ForeignKey('interest.id')))


class InterestModel(Models.Base):
    __tablename__ = 'interest'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    en = db.Column(db.String, nullable=False)
    de = db.Column(db.String, nullable=False)

    def get_name(self, lang):
        if lang and 'de' in lang:
            return self.de
        else:
            return self.en

    @staticmethod
    def from_string(string):
        return InterestModel.query.filter((InterestModel.en == string) | (InterestModel.de == string)).first()
