from contextlib import closing
from typing import Iterator

from pycs import db
from pycs.database.base import NamedBaseModel

class Collection(NamedBaseModel):

    # table columns
    project_id = db.Column(
        db.Integer, db.ForeignKey("project.id", ondelete="CASCADE"), nullable=False)

    reference = db.Column(
        db.String, nullable=False)

    description = db.Column(
        db.String)

    position = db.Column(
        db.Integer, nullable=False)

    autoselect = db.Column(
        db.Boolean, nullable=False)


    # contraints
    __table_args__ = (
        db.UniqueConstraint('project_id', 'reference'),
    )

    # relationships to other models
    files = db.relationship("File", backref="collection", lazy=True)


    def count_files(self) -> int:
        return self.files.count()
