from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, Float, Text, JSON
from sqlalchemy.orm import relationship
from database import Base

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String, unique=True, index=True)
    email = Column(String, nullable=True)
    full_name = Column(String, nullable=True)
    hashed_password = Column(String)
    disabled = Column(Boolean, default=False)

class Talent(Base):
    __tablename__ = "talents"

    id = Column(String, primary_key=True, index=True)
    idcode = Column(String, nullable=True, index=True)
    name = Column(String, index=True)
    gender = Column(String, nullable=True)
    birthDate = Column(String, nullable=True)
    title = Column(String, nullable=True)
    position = Column(String, nullable=True)
    education = Column(String, nullable=True)
    educationBackground = Column(String, nullable=True)
    address = Column(String, nullable=True)
    academicDirection = Column(String, nullable=True)
    talentPlan = Column(String, nullable=True)
    officeLocation = Column(String, nullable=True)
    email = Column(String, nullable=True)
    phone = Column(String, nullable=True)
    tutorType = Column(String, nullable=True)
    papers = Column(String, nullable=True)
    projects = Column(String, nullable=True)
    photo = Column(Text, nullable=True)
    eduWorkHistory = Column(Text, nullable=True)
    researchDirection = Column(Text, nullable=True)
    recentProjects = Column(Text, nullable=True)
    representativePapers = Column(Text, nullable=True)
    patents = Column(Text, nullable=True)
    evaluationData = Column(JSON, nullable=True)

class Lab(Base):
    __tablename__ = "labs"

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

class DashboardData(Base):
    __tablename__ = "dashboard"

    id = Column(Integer, primary_key=True)
    paperCount = Column(Integer)
    patentCount = Column(Integer)
    highImpactPapers = Column(Integer)
    keyProjects = Column(Integer)
    fundingAmount = Column(String)
    researcherStats = Column(JSON)

class News(Base):
    __tablename__ = "news"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String)
    date = Column(String)
    dashboard_id = Column(Integer, ForeignKey("dashboard.id"))
    
    dashboard = relationship("DashboardData", back_populates="news_items")

# 添加反向关系
DashboardData.news_items = relationship("News", back_populates="dashboard")

class Dimension(Base):
    __tablename__ = "dimensions"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    weight = Column(Float, default=1.0)
    category = Column(String, nullable=True)  # talent 或 lab
    description = Column(Text, nullable=True) 
    parent_id = Column(Integer, ForeignKey("dimensions.id"), nullable=True)
    
    # 自引用关系，用于树形结构
    children = relationship("Dimension", back_populates="parent", cascade="all, delete-orphan")
    parent = relationship("Dimension", back_populates="children", remote_side=[id])
    
    # 存储子维度的JSON数据
    sub_dimensions = Column(JSON, nullable=True) 