import uuid
from datetime import datetime

from pydantic import BaseModel, Field


class MonitoringRecordCreate(BaseModel):
    brand_id: uuid.UUID = Field(..., description="品牌ID")
    content_id: str | None = Field(None, description="内容ID")
    query_keywords: str | None = Field(None, description="查询关键词")
    platform: str | None = Field(None, description="平台")
    check_interval_hours: int = Field(24, description="检测间隔（小时）")


class MonitoringRecordResponse(BaseModel):
    id: uuid.UUID = Field(..., description="记录ID")
    brand_id: uuid.UUID = Field(..., description="品牌ID")
    content_id: str | None = Field(None, description="内容ID")
    query_keywords: str | None = Field(None, description="查询关键词")
    platform: str | None = Field(None, description="平台")
    baseline_citation_count: int = Field(0, description="基线引用量")
    baseline_sentiment: float | None = Field(None, description="基线情感分数")
    baseline_rank: int | None = Field(None, description="基线排名")
    current_citation_count: int = Field(0, description="当前引用量")
    current_sentiment: float | None = Field(None, description="当前情感分数")
    current_rank: int | None = Field(None, description="当前排名")
    change_type: str | None = Field(None, description="变化类型: positive/negative/neutral")
    change_details: dict | None = Field(None, description="变化详情")
    check_interval_hours: int = Field(24, description="检测间隔（小时）")
    last_checked_at: datetime | None = Field(None, description="上次检测时间")
    next_check_at: datetime | None = Field(None, description="下次检测时间")
    status: str = Field("active", description="状态: active/paused/completed")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

    class Config:
        from_attributes = True


class MonitoringRecordList(BaseModel):
    records: list[MonitoringRecordResponse] = Field(default_factory=list, description="监测记录列表")
    total: int = Field(..., description="总数")


class MonitoringChangeReport(BaseModel):
    monitoring_record_id: uuid.UUID = Field(..., description="监测记录ID")
    brand_id: uuid.UUID = Field(..., description="品牌ID")
    change_type: str = Field(..., description="变化类型: positive/negative/neutral")
    change_details: dict | None = Field(None, description="变化详情")
    baseline: dict = Field(default_factory=dict, description="基线数据")
    current: dict = Field(default_factory=dict, description="当前数据")
    recommendations: list[str] = Field(default_factory=list, description="建议")


class ContentBaselineResponse(BaseModel):
    id: uuid.UUID = Field(..., description="基线ID")
    monitoring_record_id: uuid.UUID = Field(..., description="监测记录ID")
    brand_name: str = Field(..., description="品牌名称")
    keyword: str = Field(..., description="关键词")
    platform: str = Field(..., description="平台")
    citation_count: int = Field(0, description="引用量")
    sentiment_score: float | None = Field(None, description="情感分数")
    rank_position: int | None = Field(None, description="排名位置")
    snapshot_data: dict | None = Field(None, description="快照数据")
    recorded_at: datetime = Field(..., description="记录时间")

    class Config:
        from_attributes = True


class MonitoringStatusUpdate(BaseModel):
    status: str = Field(..., description="新状态: active/paused/completed")
