import logging
from datetime import datetime

from peewee import AutoField, DateTimeField, TextField

from .base_model import BaseModel, db

db_logger = logging.getLogger("db_log")


class Log(BaseModel):
    id = AutoField(primary_key=True, unique=True, null=False)
    time = DateTimeField(unique=True, null=False, default=datetime.now)
    info_type = TextField(null=False)
    info = TextField(null=True)

    @classmethod
    @db.atomic()
    def log(cls, info_type, info=None):
        cls.create(info_type=info_type, info=info)
        db_logger.info(f"{info_type}: {info}")

    class Meta:
        table_name = "log"
