from typing import Optional, List, Any, Dict
from datetime import datetime
from pydantic import BaseModel
from enum import Enum

class ActionType(str, Enum):
    MANUAL_ADD = "MANUAL_ADD"
    DELETE = "DELETE"
    UPDATE = "UPDATE"
    IMPORT = "IMPORT"
    DISABLE = "DISABLE"
    ENABLE = "ENABLE"
    RESET_PASSWORD = "RESET_PASSWORD"
    CHANGE_ROLE = "CHANGE_ROLE"
    TRANSFER = "TRANSFER"
    VIEW_SECRET = "VIEW_SECRET"
    REGENERATE_SECRET = "REGENERATE_SECRET"
    SYNC_M2M = "SYNC_M2M"
    SYNC = "SYNC"
    ORG_CREATE = "ORG_CREATE"
    ORG_UPDATE = "ORG_UPDATE"
    API_KEY_CREATE = "API_KEY_CREATE"
    API_KEY_UPDATE = "API_KEY_UPDATE"
    API_KEY_DISABLE = "API_KEY_DISABLE"
    API_KEY_ENABLE = "API_KEY_ENABLE"
    API_KEY_REVOKE = "API_KEY_REVOKE"

class OperationLogBase(BaseModel):
    app_id: Optional[int] = None
    action_type: ActionType
    target_mobile: Optional[str] = None
    ip_address: Optional[str] = None
    details: Optional[Dict[str, Any]] = None

class OperationLogCreate(OperationLogBase):
    operator_id: int
    target_user_id: Optional[int] = None

class OperationLogResponse(OperationLogBase):
    id: int
    operator_id: int
    operator_mobile: Optional[str] = None # Enriched
    target_user_id: Optional[int] = None
    created_at: datetime

    class Config:
        from_attributes = True

class OperationLogList(BaseModel):
    total: int
    items: List[OperationLogResponse]
