from ..extensions import db
from ..tools.data import declutter_results
from ..tools.models import JsonString

from sqlalchemy_json import MutableJson

from datetime import datetime
from uuid import uuid4

class Entry(db.Model):

    id = db.Column(db.String(36), primary_key=True)
    timestamp = db.Column(db.DateTime, nullable=False)
    answers = db.Column(JsonString, nullable=False)
    results = db.Column(MutableJson, nullable=False)

    def __repr__(self) -> str: return f'Entry with <id {self.id}>.'

    def __init__(self, answers:list, results:dict):
        self.id = uuid4().hex
        self.timestamp = datetime.utcnow()
        self.answers = answers.copy()
        self.results = declutter_results(results)

    def add(self):
        db.session.add(self)
        db.session.commit()