"""
SCRM 系统对接 API Schema 定义

用于 SCRM 系统调用考陪练系统的数据查询接口
"""

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


# ==================== 通用响应 ====================

class SCRMBaseResponse(BaseModel):
    """SCRM API 通用响应基类"""
    code: int = Field(default=0, description="响应码，0=成功")
    message: str = Field(default="success", description="响应消息")


# ==================== 1. 获取员工岗位 ====================

class PositionInfo(BaseModel):
    """岗位信息"""
    position_id: int = Field(..., description="岗位ID")
    position_name: str = Field(..., description="岗位名称")
    is_primary: bool = Field(default=True, description="是否主岗位")
    joined_at: Optional[str] = Field(None, description="加入时间")


class EmployeePositionData(BaseModel):
    """员工岗位数据"""
    employee_id: int = Field(..., description="员工ID")
    userid: Optional[str] = Field(None, description="企微员工userid（可能为空）")
    name: str = Field(..., description="员工姓名")
    positions: List[PositionInfo] = Field(default=[], description="岗位列表")


class EmployeePositionResponse(SCRMBaseResponse):
    """获取员工岗位响应"""
    data: Optional[EmployeePositionData] = None


# ==================== 2. 获取岗位课程 ====================

class CourseInfo(BaseModel):
    """课程信息"""
    course_id: int = Field(..., description="课程ID")
    course_name: str = Field(..., description="课程名称")
    course_type: str = Field(..., description="课程类型：required/optional")
    priority: int = Field(default=0, description="优先级")
    knowledge_point_count: int = Field(default=0, description="知识点数量")


class PositionCoursesData(BaseModel):
    """岗位课程数据"""
    position_id: int = Field(..., description="岗位ID")
    position_name: str = Field(..., description="岗位名称")
    courses: List[CourseInfo] = Field(default=[], description="课程列表")


class PositionCoursesResponse(SCRMBaseResponse):
    """获取岗位课程响应"""
    data: Optional[PositionCoursesData] = None


# ==================== 3. 搜索知识点 ====================

class KnowledgePointSearchRequest(BaseModel):
    """搜索知识点请求"""
    keywords: List[str] = Field(..., min_length=1, description="搜索关键词列表")
    position_id: Optional[int] = Field(None, description="岗位ID（用于优先排序）")
    course_ids: Optional[List[int]] = Field(None, description="限定课程范围")
    knowledge_type: Optional[str] = Field(None, description="知识点类型筛选")
    limit: int = Field(default=10, ge=1, le=100, description="返回数量")


class KnowledgePointBrief(BaseModel):
    """知识点简要信息"""
    knowledge_point_id: int = Field(..., description="知识点ID")
    name: str = Field(..., description="知识点名称")
    course_id: int = Field(..., description="课程ID")
    course_name: str = Field(..., description="课程名称")
    type: str = Field(..., description="知识点类型")
    relevance_score: float = Field(default=1.0, description="相关度分数")


class KnowledgePointSearchData(BaseModel):
    """知识点搜索结果数据"""
    total: int = Field(..., description="匹配总数")
    items: List[KnowledgePointBrief] = Field(default=[], description="知识点列表")


class KnowledgePointSearchResponse(SCRMBaseResponse):
    """搜索知识点响应"""
    data: Optional[KnowledgePointSearchData] = None


# ==================== 4. 获取知识点详情 ====================

class KnowledgePointDetailData(BaseModel):
    """知识点详情数据"""
    knowledge_point_id: int = Field(..., description="知识点ID")
    name: str = Field(..., description="知识点名称")
    course_id: int = Field(..., description="课程ID")
    course_name: str = Field(..., description="课程名称")
    type: str = Field(..., description="知识点类型")
    content: str = Field(..., description="知识点完整内容（description）")
    material_id: Optional[int] = Field(None, description="关联的课程资料ID")
    material_type: Optional[str] = Field(None, description="资料文件类型")
    material_url: Optional[str] = Field(None, description="资料文件URL")
    topic_relation: Optional[str] = Field(None, description="与主题的关系描述")
    source: int = Field(default=0, description="来源：0=手动创建，1=AI分析生成")
    created_at: Optional[str] = Field(None, description="创建时间")


class KnowledgePointDetailResponse(SCRMBaseResponse):
    """获取知识点详情响应"""
    data: Optional[KnowledgePointDetailData] = None


# ==================== 错误响应 ====================

class SCRMErrorResponse(SCRMBaseResponse):
    """错误响应"""
    code: int = Field(..., description="错误码")
    message: str = Field(..., description="错误消息")
    data: None = None

