"""ScoDoc 9 models : Formation BUT 2021
"""
from enum import unique
from typing import Any

from app import db

from app.scodoc.sco_utils import ModuleType

Modules_ACs = db.Table(
    "modules_acs",
    db.Column("module_id", db.ForeignKey("notes_modules.id")),
    db.Column("ac_id", db.ForeignKey("app_crit.id")),
)


class AppCrit(db.Model):
    "Apprentissage Critique BUT"
    __tablename__ = "app_crit"
    id = db.Column(db.Integer, primary_key=True)
    code = db.Column(db.Text(), nullable=False, info={"label": "Code"})
    titre = db.Column(db.Text(), info={"label": "Titre"})

    modules = db.relationship(
        "Module", secondary=Modules_ACs, lazy="dynamic", backref="acs"
    )

    def to_dict(self):
        result = dict(self.__dict__)
        result.pop("_sa_instance_state", None)
        return result

    def get_label(self):
        return self.code + " - " + self.titre

    def __repr__(self):
        return "<AC {}>".format(self.code)

    def get_saes(self):
        """Liste des SAE associées"""
        return [m for m in self.modules if m.module_type == ModuleType.SAE]
