from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime

class IDGThemeSetting(BaseModel):
    key: str
    value: str
    updatedAt: datetime = Field(default_factory=datetime.utcnow)

class IDGWidgetBlock(BaseModel):
    blockKey: str
    title: str
    type: str = "html" # html, iframe, internal
    content: str
    area: str = "sidebar"
    position: int = 0
    active: bool = True
    updatedAt: datetime = Field(default_factory=datetime.utcnow)

class IDGWidgetCache(BaseModel):
    blockKey: str
    cacheKey: str
    html: str
    expiresAt: datetime

class IDGLayoutBlock(BaseModel):
    page: str
    area: str
    blockId: str
    order: int = 0
    width: Optional[str] = None
    height: Optional[str] = None

class IDGMenuItem(BaseModel):
    key: str
    parentKey: Optional[str] = None
    title: str
    link: str
    icon: Optional[str] = None
    order: int = 0
    active: bool = True

class IDGAdminLog(BaseModel):
    action: str
    adminId: str
    details: Dict[str, Any] = {}
    createdAt: datetime = Field(default_factory=datetime.utcnow)

async def create_idg_indexes(db):
    # idg_theme_settings.key unique
    await db.idg_theme_settings.create_index("key", unique=True)
    
    # idg_widget_blocks.blockKey unique
    await db.idg_widget_blocks.create_index("blockKey", unique=True)
    # idg_widget_blocks.area + position
    await db.idg_widget_blocks.create_index([("area", 1), ("position", 1)])
    
    # idg_widget_cache.blockKey + cacheKey unique
    await db.idg_widget_cache.create_index([("blockKey", 1), ("cacheKey", 1)], unique=True)
    # idg_widget_cache.expiresAt TTL index
    await db.idg_widget_cache.create_index("expiresAt", expireAfterSeconds=0)
    
    # idg_layout_blocks.page + area + order
    await db.idg_layout_blocks.create_index([("page", 1), ("area", 1), ("order", 1)])
    
    # idg_menu_items.parentKey + order
    await db.idg_menu_items.create_index([("parentKey", 1), ("order", 1)])
    
    # idg_admin_logs.createdAt
    await db.idg_admin_logs.create_index("createdAt")
