"""平台规则管理 Schema - 定义规则管理的请求响应结构"""

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


# ============================================================
# 基础信息 Schema
# ============================================================

class ContentLengthRule(BaseModel):
    """内容长度规则"""
    min: int = Field(..., description="最小字数")
    max: int = Field(..., description="最大字数")
    recommended: int = Field(..., description="推荐字数")


class StructurePreference(BaseModel):
    """结构偏好"""
    has_intro: bool = True
    has_conclusion: bool = True
    has_toc: bool = False


# ============================================================
# 标题规则 Schema
# ============================================================

class TitleRule(BaseModel):
    """标题规则"""
    min_length: int = Field(..., description="最小长度")
    max_length: int = Field(..., description="最大长度")
    avoid_patterns: list[str] = Field(default_factory=list, description="避免的模式")
    required_patterns: list[str] = Field(default_factory=list, description="需要的模式")
    case_style: str = Field(default="normal", description="大小写样式")


# ============================================================
# 标签规则 Schema
# ============================================================

class TagRule(BaseModel):
    """标签规则"""
    min_tags: int = Field(..., description="最少标签数")
    max_tags: int = Field(..., description="最多标签数")
    tag_style: str = Field(default="plain", description="标签样式: plain/hashtag/comma")


# ============================================================
# AI 敏感度 Schema
# ============================================================

class AISensitivity(BaseModel):
    """AI敏感度配置"""
    detection_level: str = Field(..., description="检测级别: high/medium/low")
    banned_patterns: list[str] = Field(default_factory=list, description="禁用模式")
    banned_structures: list[str] = Field(default_factory=list, description="禁用结构")
    safe_patterns: list[str] = Field(default_factory=list, description="安全模式")
    humanization_required: bool = Field(default=False, description="是否需要去AI化")


# ============================================================
# 敏感词规则 Schema
# ============================================================

class SensitiveWordsConfig(BaseModel):
    """敏感词配置"""
    check_required: bool = Field(default=True, description="是否需要检查")
    categories: list[str] = Field(default_factory=list, description="敏感词分类")
    max_tolerance: int = Field(default=0, description="容忍度")
    auto_filter: bool = Field(default=True, description="自动过滤")


# ============================================================
# SEO 规则 Schema
# ============================================================

class KeywordDensity(BaseModel):
    """关键词密度"""
    min: float = Field(..., description="最小百分比")
    max: float = Field(..., description="最大百分比")
    recommended: float = Field(..., description="推荐百分比")


class SEORule(BaseModel):
    """SEO规则"""
    keyword_density: KeywordDensity
    keyword_position: list[str] = Field(default_factory=list, description="关键词位置")
    internal_links: dict = Field(default_factory=dict, description="内链规则")


# ============================================================
# GEO 规则 Schema
# ============================================================

class GEORule(BaseModel):
    """GEO规则"""
    citation_format: str = Field(default="plain", description="引用格式: plain/link/markdown/academic")
    source_attribution: bool = Field(default=False, description="是否需要来源标注")
    reference_style: str = Field(default="informal", description="引用风格: academic/informal")


# ============================================================
# HTML 规则 Schema
# ============================================================

class HTMLRule(BaseModel):
    """HTML规则"""
    supported_tags: list[str] = Field(default_factory=list, description="支持的HTML标签")
    banned_tags: list[str] = Field(default_factory=list, description="禁用的HTML标签")
    image_support: bool = Field(default=True, description="是否支持图片")
    video_support: bool = Field(default=False, description="是否支持视频")
    code_block_support: bool = Field(default=False, description="是否支持代码块")


# ============================================================
# 发布规则 Schema
# ============================================================

class PublishRule(BaseModel):
    """发布规则"""
    auto_publish: bool = Field(default=False, description="是否自动发布")
    require_review: bool = Field(default=True, description="是否需要审核")
    publish_timing: str = Field(default="immediate", description="发布时机: immediate/scheduled/draft")


# ============================================================
# 完整平台规则 Schema
# ============================================================

class PlatformRuleSchema(BaseModel):
    """完整平台规则"""
    platform_name: str = Field(..., description="平台名称")
    platform_type: str = Field(default="", description="平台类型")
    priority: str = Field(default="P2", description="优先级: P0/P1/P2")
    enabled: bool = Field(default=True, description="是否启用")
    content_style: str = Field(default="", description="内容风格")
    content_length: ContentLengthRule
    structure_preference: StructurePreference
    title_rules: TitleRule
    tag_rules: TagRule
    ai_sensitivity: AISensitivity
    sensitive_words: SensitiveWordsConfig
    seo_rules: SEORule
    geo_rules: GEORule
    html_rules: HTMLRule
    publish_rules: PublishRule


# ============================================================
# 简化的平台信息 Schema (用于列表展示)
# ============================================================

class PlatformBrief(BaseModel):
    """简化的平台信息"""
    id: str
    name: str
    platform_type: str = ""
    priority: str = "P2"
    enabled: bool = True


class PlatformListResponse(BaseModel):
    """平台列表响应"""
    platforms: list[PlatformBrief]
    total: int


class PlatformDetailResponse(BaseModel):
    """平台详情响应"""
    id: str
    name: str
    platform_type: str = ""
    priority: str = "P2"
    enabled: bool = True
    content_style: str = ""
    content_length: ContentLengthRule
    structure_preference: StructurePreference
    title_rules: TitleRule
    tag_rules: TagRule
    ai_sensitivity: AISensitivity
    sensitive_words: SensitiveWordsConfig
    seo_rules: SEORule
    geo_rules: GEORule
    html_rules: HTMLRule
    publish_rules: PublishRule
    best_publish_times: list[str] = []
    best_publish_days: list[str] = []
    max_images: int = 0


# ============================================================
# 规则更新 Schema
# ============================================================

class PlatformRuleUpdateRequest(BaseModel):
    """平台规则更新请求"""
    content_style: Optional[str] = None
    content_length: Optional[ContentLengthRule] = None
    structure_preference: Optional[StructurePreference] = None
    title_rules: Optional[TitleRule] = None
    tag_rules: Optional[TagRule] = None
    ai_sensitivity: Optional[AISensitivity] = None
    sensitive_words: Optional[SensitiveWordsConfig] = None
    seo_rules: Optional[SEORule] = None
    geo_rules: Optional[GEORule] = None
    html_rules: Optional[HTMLRule] = None
    publish_rules: Optional[PublishRule] = None
    enabled: Optional[bool] = None


class PlatformRuleUpdateResponse(BaseModel):
    """平台规则更新响应"""
    success: bool
    platform_id: str
    message: str
    updated_at: datetime


# ============================================================
# 规则变更历史 Schema
# ============================================================

class RuleChangeHistory(BaseModel):
    """规则变更历史"""
    id: int
    version: int = 0
    platform_id: str
    platform_name: str
    changed_by: str
    change_summary: str
    change_type: str
    previous_rules: Optional[dict] = None
    new_rules: Optional[dict] = None
    created_at: datetime


class RuleChangeHistoryResponse(BaseModel):
    """规则变更历史响应"""
    history: list[RuleChangeHistory]
    total: int


# ============================================================
# 规则验证 Schema
# ============================================================

class ContentValidationIssue(BaseModel):
    """内容验证问题"""
    severity: str = Field(..., description="严重程度: high/medium/low")
    message: str = Field(..., description="问题描述")
    category: str = Field(..., description="问题分类")


class ContentValidationResponse(BaseModel):
    """内容验证响应"""
    is_valid: bool
    score: int = Field(..., ge=0, le=100, description="合规分数 0-100")
    issues: list[ContentValidationIssue]
    passed: list[str]


class ContentValidateRequest(BaseModel):
    """内容验证请求"""
    content: str = Field(..., description="待验证内容")
    title: str = Field(..., description="标题")
    platform: str = Field(..., description="平台ID")


# ============================================================
# 规则差异对比 Schema
# ============================================================

class RuleDiff(BaseModel):
    """规则差异"""
    field: str
    old_value: Optional[Any] = None
    new_value: Optional[Any] = None


class RuleDiffResponse(BaseModel):
    """规则差异响应"""
    platform_id: str
    platform_name: str
    diffs: list[RuleDiff]
    total_changes: int


# ============================================================
# AI 去AI化请求 Schema
# ============================================================

class DeAIContentRequest(BaseModel):
    """去AI化请求"""
    content: str = Field(..., description="待处理内容")
    platform: str = Field(..., description="目标平台")
    style: Optional[str] = Field(default="自然流畅", description="目标风格")


class DeAIContentResponse(BaseModel):
    """去AI化响应"""
    original_content: str
    processed_content: str
    original_word_count: int
    processed_word_count: int
    detected_ai_patterns: list[str] = []
    replaced_patterns: dict[str, str] = {}


RuleDiff.model_rebuild()
RuleDiffResponse.model_rebuild()
