from pydantic import BaseModel
from typing import List, Optional, Dict, Any

# 用户模型
class UserBase(BaseModel):
    username: str
    email: Optional[str] = None
    full_name: Optional[str] = None

class UserCreate(UserBase):
    password: str

class User(UserBase):
    id: int
    disabled: Optional[bool] = None

    class Config:
        from_attributes = True

# 令牌模型
class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: str

# 人才模型
class TalentBase(BaseModel):
    name: str
    idcode: Optional[str] = None  # 新增展示用ID字段
    gender: Optional[str] = None
    birthDate: Optional[str] = None
    title: Optional[str] = None
    position: Optional[str] = None
    education: Optional[str] = None
    educationBackground: Optional[str] = None
    address: Optional[str] = None
    academicDirection: Optional[str] = None
    talentPlan: Optional[str] = None
    officeLocation: Optional[str] = None
    email: Optional[str] = None
    phone: Optional[str] = None
    tutorType: Optional[str] = None
    papers: Optional[str] = None
    projects: Optional[str] = None
    photo: Optional[str] = None
    eduWorkHistory: Optional[str] = None
    researchDirection: Optional[str] = None
    recentProjects: Optional[str] = None
    representativePapers: Optional[str] = None
    patents: Optional[str] = None
    evaluationData: Optional[List[float]] = None

class TalentCreate(TalentBase):
    id: str

class Talent(TalentBase):
    id: str

    class Config:
        from_attributes = True

# 工程研究中心模型
class LabBase(BaseModel):
    name: str
    idcode: Optional[str] = None  # 新增展示用ID字段
    
    # 基本信息
    center_number: Optional[str] = None  # 中心编号
    school: Optional[str] = None  # 所属学校
    department: Optional[str] = None  # 主管部门
    field: Optional[str] = None  # 所属领域
    
    # 年度数据 - 修改为可以接受字符串或字典列表
    annual_data: Optional[str] = None  # 存储为JSON字符串
    current_year: Optional[str] = None  # 当前评估年份
    
    # 详细信息
    innovation_situation: Optional[str] = None  # 技术攻关与创新情况
    overall_situation: Optional[str] = None  # 总体情况
    engineering_cases: Optional[str] = None  # 工程化案例
    industry_service: Optional[str] = None  # 行业服务情况
    discipline_support: Optional[str] = None  # 学科发展支撑情况
    talent_cultivation: Optional[str] = None  # 人才培养情况
    team_building: Optional[str] = None  # 研究队伍建设情况
    department_support: Optional[str] = None  # 主管部门、依托单位支持情况
    equipment_sharing: Optional[str] = None  # 仪器设备开放共享情况
    academic_style: Optional[str] = None  # 学风建设情况
    technical_committee: Optional[str] = None  # 技术委员会工作情况
    next_year_plan: Optional[str] = None  # 下一年度工作计划
    problems_suggestions: Optional[str] = None  # 问题与建议
    director_opinion: Optional[str] = None  # 工程中心负责人意见
    institution_opinion: Optional[str] = None  # 依托单位意见
    research_directions: Optional[str] = None  # 研究方向/学术带头人
    
    # 统计数据
    national_awards_first: Optional[int] = 0  # 国家级科技奖励一等奖
    national_awards_second: Optional[int] = 0  # 国家级科技奖励二等奖
    provincial_awards_first: Optional[int] = 0  # 省、部级科技奖励一等奖
    provincial_awards_second: Optional[int] = 0  # 省、部级科技奖励二等奖
    valid_patents: Optional[int] = 0  # 有效专利
    other_ip: Optional[int] = 0  # 其他知识产权
    international_standards: Optional[int] = 0  # 国际/国家标准
    industry_standards: Optional[int] = 0  # 行业/地方标准
    
    # 专利转化相关
    patent_transfer_contracts: Optional[int] = 0  # 专利转让合同项数
    patent_transfer_amount: Optional[float] = 0.0  # 专利转让合同金额
    patent_license_contracts: Optional[int] = 0  # 专利许可合同项数
    patent_license_amount: Optional[float] = 0.0  # 专利许可合同金额
    patent_valuation_contracts: Optional[int] = 0  # 专利作价合同项数
    patent_valuation_amount: Optional[float] = 0.0  # 专利作价金额
    
    # 项目合作
    project_contracts: Optional[int] = 0  # 项目合同项数
    project_amount: Optional[float] = 0.0  # 项目合同金额
    
    # 学科信息
    discipline_1: Optional[str] = None  # 依托学科1
    discipline_2: Optional[str] = None  # 依托学科2
    discipline_3: Optional[str] = None  # 依托学科3
    
    # 人才培养数据
    doctoral_students: Optional[int] = 0  # 在读博士生
    master_students: Optional[int] = 0  # 在读硕士生
    graduated_doctoral: Optional[int] = 0  # 当年毕业博士
    graduated_master: Optional[int] = 0  # 当年毕业硕士
    undergraduate_courses: Optional[int] = 0  # 承担本科课程
    graduate_courses: Optional[int] = 0  # 承担研究生课程
    textbooks: Optional[int] = 0  # 大专院校教材
    
    # 人员结构
    professors: Optional[int] = 0  # 教授人数
    associate_professors: Optional[int] = 0  # 副教授人数
    lecturers: Optional[int] = 0  # 讲师人数
    domestic_visitors: Optional[int] = 0  # 国内访问学者
    foreign_visitors: Optional[int] = 0  # 国外访问学者
    postdoc_in: Optional[int] = 0  # 本年度进站博士后
    postdoc_out: Optional[int] = 0  # 本年度出站博士后
    
    # 基础设施
    center_area: Optional[float] = 0.0  # 工程中心面积
    new_area: Optional[float] = 0.0  # 当年新增面积
    fixed_personnel: Optional[int] = 0  # 固定人员
    mobile_personnel: Optional[int] = 0  # 流动人员
    
    # 经费情况
    total_funding: Optional[float] = 0.0  # 当年项目到账总经费
    vertical_funding: Optional[float] = 0.0  # 纵向经费
    horizontal_funding: Optional[float] = 0.0  # 横向经费
    
    # 服务情况
    technical_consultations: Optional[int] = 0  # 技术咨询次数
    training_services: Optional[int] = 0  # 培训服务人次
    
    # 原有字段保留兼容性
    personnel: Optional[str] = None
    nationalProjects: Optional[str] = None
    otherProjects: Optional[str] = None
    achievements: Optional[str] = None
    labAchievements: Optional[str] = None
    image: Optional[str] = None
    score: Optional[int] = None
    evaluationData: Optional[List[float]] = None
    sub_dimension_evaluations: Optional[Dict[str, Any]] = None  # 存储二级维度评估数据

class LabCreate(LabBase):
    id: str

class Lab(LabBase):
    id: str

    class Config:
        from_attributes = True

# 新闻模型
class NewsBase(BaseModel):
    title: str
    date: str

class NewsCreate(NewsBase):
    dashboard_id: int

class News(NewsBase):
    id: int
    dashboard_id: int

    class Config:
        from_attributes = True

# 仪表盘数据模型
class DashboardData(BaseModel):
    paperCount: int
    patentCount: int
    highImpactPapers: int
    keyProjects: int
    fundingAmount: str
    researcherStats: Dict[str, int]
    newsData: List[Dict[str, str]]

    class Config:
        from_attributes = True

# URL抓取请求模型
class ScrapeRequest(BaseModel):
    url: str

# 保存评估数据请求模型
class SaveDataRequest(BaseModel):
    data_type: str  # "talent" 或 "lab"
    data: Dict[str, Any]

# 维度模型
class SubDimensionBase(BaseModel):
    name: str
    weight: float = 1.0
    description: Optional[str] = None

class SubDimension(SubDimensionBase):
    id: Optional[int] = None
    
    class Config:
        from_attributes = True

class DimensionBase(BaseModel):
    name: str
    weight: float = 0.0  # 一级维度不需要权重
    category: Optional[str] = None
    description: Optional[str] = None
    sub_dimensions: Optional[List[SubDimension]] = None
    subDimensions: Optional[List[SubDimension]] = None  # 添加subDimensions作为别名，兼容前端

class DimensionCreate(DimensionBase):
    pass

class Dimension(DimensionBase):
    id: int

    class Config:
        from_attributes = True

# 批量保存维度的请求模型
class SaveDimensionsRequest(BaseModel):
    dimensions: List[DimensionBase]
    category: str  # "talent" 或 "lab" 