from pydantic import BaseModel, ConfigDict
from datetime import datetime, date
from typing import Optional, List, Dict, Any

class AuditLogBase(BaseModel):
    user_id: Optional[int] = None
    username: str
    action: str
    resource_type: Optional[str] = None
    resource_id: Optional[str] = None
    details: Optional[Dict[str, Any]] = None
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    created_at: Optional[datetime] = None

class AuditLogResponse(AuditLogBase):
    id: int

    model_config = ConfigDict(from_attributes=True)


class UserBase(BaseModel):
    username: str
    role: str = "user"

class UserCreate(UserBase):
    password: str

class UserUpdate(BaseModel):
    username: Optional[str] = None
    password: Optional[str] = None
    role: Optional[str] = None

class UserResponse(UserBase):
    id: int
    created_at: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)

class LoginRequest(BaseModel):
    username: str
    password: str

class TokenResponse(BaseModel):
    token: str
    role: str
    user: UserResponse

class TopicBase(BaseModel):
    id: str
    title: str
    field: str
    priority_score: int = 0
    status: str = "pending"
    compliance_score: Optional[int] = None
    ready_at: Optional[date] = None
    published_at: Optional[date] = None
    generated_at: Optional[datetime] = None
    platform_urls: Optional[Dict[str, str]] = None

class TopicResponse(TopicBase):
    created_at: Optional[datetime] = None
    updated_at: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)

class ArticleBase(BaseModel):
    id: str
    topic_id: str
    platform: str
    file_path: str
    status: str = "draft"
    compliance_score: Optional[int] = None
    created_at: Optional[datetime] = None

class ArticleResponse(ArticleBase):
    model_config = ConfigDict(from_attributes=True)

class SystemStatus(BaseModel):
    total_topics: int
    topics_by_status: Dict[str, int]
    ready_topics: List[TopicResponse]
    today_articles: int
    compliance_rate: float
    last_optimization: Optional[datetime] = None
    execution_time: Optional[float] = None

class OptimizationRequest(BaseModel):
    topic_ids: Optional[List[str]] = None

class PublishRequest(BaseModel):
    topic_id: str
    platform_urls: Dict[str, str]

class PublishActionRequest(BaseModel):
    action: str
    platform: Optional[str] = None
    status: str
    operator: Optional[str] = None
    description: Optional[str] = None
    suggestion: Optional[str] = None
    url: Optional[str] = None
    error_msg: Optional[str] = None

class PublishRecordResponse(BaseModel):
    id: int
    topic_id: str
    platform: str
    action: str
    status: str
    operator: Optional[str] = None
    description: Optional[str] = None
    suggestion: Optional[str] = None
    url: Optional[str] = None
    error_msg: Optional[str] = None
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)

class BatchPublishRequest(BaseModel):
    date: str


class AuditLogResponse(BaseModel):
    id: int
    user_id: Optional[int] = None
    username: str
    action: str
    resource_type: Optional[str] = None
    resource_id: Optional[str] = None
    details: Optional[Dict[str, Any]] = None
    ip_address: Optional[str] = None
    user_agent: Optional[str] = None
    created_at: datetime

    model_config = ConfigDict(from_attributes=True)

