import uuid

from sqlalchemy.dialects.postgresql import UUID

import Models
from app import db


class CardModel(Models.Base):
    __tablename__ = 'card'

    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    type = db.Column(db.String, nullable=False)
    user_id = db.Column(db.ForeignKey('user.id'), nullable=False)
    user = db.relationship(
        "UserModel",
        foreign_keys=user_id,
        back_populates="cards"
    )
    image_id = db.Column(db.ForeignKey('image.id'), nullable=False)
    image = db.relationship(
        "ImageModel",
        foreign_keys=image_id
    )
    panels = db.relationship(
        "PanelModel",
        order_by="asc(PanelModel.position)",
        cascade="all, delete"
    )

    def image_caption(self, lang):
        return self.user.display_name

    def image_tag(self, lang):
        return '@' + self.user.username

    def image_text(self, lang):
        text = []
        if self.user.privacy_settings.show_course_of_study:
            text.append(self.user.personal_information.course_of_study)
        if self.user.privacy_settings.show_matriculation_date:
            text.append(f"{self.user.personal_information.current_semester}. Semester")
        return "\n".join(text)
