from datetime import datetime
from typing import Optional, Union

from pydantic import BaseModel, Field


class BaseCodegen(BaseModel):
    name: Optional[str] = Field(description="名称")
    author: Optional[str] = Field(description="作者", default='')
    description: Optional[str] = Field(description="描述", default='')
    version: Optional[str] = Field(description="版本", default='')
    fields: Optional[Union[str, dict]] = Field(description="字段", default='{}')
    relations: Optional[Union[str, dict]] = Field(description="关系", default='{}')
    api: Optional[Union[str, dict]] = Field(description="API", default='{}')
    menu: Optional[Union[str, dict]] = Field(description="菜单", default='{}')


class CodegenCreate(BaseCodegen):
    name: str = Field(description="名称")
    fields: str = Field(description="字段")

class CodegenUpdate(BaseModel):
    id: int = Field(example=1)
    name: str = Field(example="管理员")
    desc: str = Field("", example="管理员角色")


class CodegenUpdateMenusApis(BaseModel):
    id: int
    menu_ids: list[int] = []
    api_infos: list[dict] = []


class DatasourceInfo(BaseModel):
    id: int = Field(..., description="ID")
    tableName: Optional[str] = Field(..., description="名称")
    tableComment: Optional[str] = Field(..., description="注释")
    created_at: Optional[datetime]
    updated_at: Optional[datetime]


class DatasourceCreate(DatasourceInfo):
    tableName: str = Field(..., description="名称")
    tableComment: str = Field(..., description="注释")


class DatasourceUpdate(DatasourceInfo):
    id: int = Field(..., description="ID")
