from datetime import datetime
from typing import Optional

class LogModel:
    def __init__(self,
                 username: str,
                 operation_type: str,
                 operation_desc: str,
                 operation_result: Optional[int] = None,
                 operation_module: Optional[str] = None,
                 operation_data: Optional[str] = None,
                 operation_ip: Optional[str] = None,
                 data_changes: Optional[str] = None,
                 log_id: Optional[int] = None,
                 created_at: Optional[datetime] = None):
        self.id = log_id
        self.username = username
        self.operation_type = operation_type
        self.operation_desc = operation_desc
        self.operation_result = operation_result
        self.operation_module = operation_module
        self.operation_data = operation_data
        self.operation_ip = operation_ip
        self.data_changes = data_changes
        self.created_at = created_at or datetime.now()

    def to_dict(self) -> dict:
        return {
            'id': self.id,
            'username': self.username,
            'operation_type': self.operation_type,
            'operation_desc': self.operation_desc,
            'operation_result': self.operation_result,
            'operation_module': self.operation_module,
            'operation_data': self.operation_data,
            'operation_ip': self.operation_ip,
            'data_changes': self.data_changes,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S') if self.created_at else None
        }