import json

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

class Model(NamedBaseModel):
    """
    database class for ML Models
    """

    description = db.Column(db.String)
    root_folder = db.Column(db.String, nullable=False, unique=True)
    supports_encoded = db.Column(db.String, nullable=False)

    # relationships to other models
    projects = db.relationship("Project", backref="model", lazy=True)

    @property
    def supports(self):
        return json.loads(self.supports_encoded)


    @commit_on_return
    def copy_to(self, new_name: str, new_root_folder: str):

        model = Model.query.get(root_folder=new_root_folder)
        is_new = False

        if model is None:
            model = Model.new(root_folder=new_root_folder)
            is_new = True

        model.name = name
        model.description = self.description
        model.supports_encoded = self.supports_encoded

        return model, is_new
