"""
MySQL数据模型定义

提供MySQL数据库表的结构化模型定义
"""

from typing import Optional, Dict, Any, List
from dataclasses import dataclass
from datetime import datetime


@dataclass
class UserModel:
    """用户模型"""
    id: Optional[int] = None
    name: str = ""
    email: str = ""
    age: int = 0
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'email': self.email,
            'age': self.age,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'UserModel':
        """从字典创建实例"""
        return cls(
            id=data.get('id'),
            name=data.get('name', ''),
            email=data.get('email', ''),
            age=data.get('age', 0),
            created_at=datetime.fromisoformat(data['created_at']) if data.get('created_at') else None,
            updated_at=datetime.fromisoformat(data['updated_at']) if data.get('updated_at') else None
        )


@dataclass
class TestTableModel:
    """测试表模型"""
    id: Optional[int] = None
    name: str = ""
    description: Optional[str] = None
    status: str = "active"
    created_at: Optional[datetime] = None

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'TestTableModel':
        """从字典创建实例"""
        return cls(
            id=data.get('id'),
            name=data.get('name', ''),
            description=data.get('description'),
            status=data.get('status', 'active'),
            created_at=datetime.fromisoformat(data['created_at']) if data.get('created_at') else None
        )


@dataclass
class BasisOfPreparationModel:
    """编制依据模型"""
    id: Optional[int] = None
    title: str = ""
    content: Optional[str] = None
    category: Optional[str] = None
    status: str = "current"
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

    def to_dict(self) -> Dict[str, Any]:
        """转换为字典"""
        return {
            'id': self.id,
            'title': self.title,
            'content': self.content,
            'category': self.category,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }

    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'BasisOfPreparationModel':
        """从字典创建实例"""
        return cls(
            id=data.get('id'),
            title=data.get('title', ''),
            content=data.get('content'),
            category=data.get('category'),
            status=data.get('status', 'current'),
            created_at=datetime.fromisoformat(data['created_at']) if data.get('created_at') else None,
            updated_at=datetime.fromisoformat(data['updated_at']) if data.get('updated_at') else None
        )


__all__ = [
    "UserModel",
    "TestTableModel",
    "BasisOfPreparationModel"
]