from datetime import datetime

from peewee import (AutoField, BooleanField, DateTimeField, ForeignKeyField,
                    TextField, IntegerField)
from playhouse.sqlite_ext import JSONField

from .base_model import BaseModel, db
from .users import Users


class Archive(BaseModel):
    id = AutoField(primary_key=True, unique=True, null=False)
    time = DateTimeField(unique=True, null=False, default=datetime.now())
    user = ForeignKeyField(Users, Users.username, null=False)
    result = BooleanField(null=False)
    overridden = BooleanField(null=False)
    test_data = JSONField(null=False)
    label = TextField(null=True)
    archived = IntegerField(null=False, default=False)
    uploaded = BooleanField(null=False, default=False)

    @classmethod
    @db.atomic()
    def archive(cls, test_data, result, overridden):
        time=datetime.now()
        test_data["time"]=time.strftime("%d/%m/%Y %H:%M:%S")
        test_data["user"]=Users.get_session().username
        return cls.create(
            time=time,
            user=Users.get_session().user,
            result=result,
            overridden=overridden,
            test_data=test_data,
        )

    class Meta:
        table_name = "archive"
