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


class DbKgProj(Base):
    __tablename__ = "kg_projs"
    
    id = Column(Integer, primary_key=True, index=True)
    proj_name = Column(String(64), nullable=False)
    proj_category = Column(String(64), nullable=False)
    proj_type = Column(String(64), nullable=False)
    proj_conf = Column(String(300), nullable=False)
    created = Column(DateTime, nullable=False)
    updated = Column(DateTime, nullable=False)
    status = Column(Integer, default=0)


class DbKgGraphs(Base):
    __tablename__ = "kg_graphs"

    id = Column(Integer, primary_key=True, index=True)
    category = Column(String(64), nullable=False)
    name = Column(String(64), nullable=False)
    graph_description = Column(String(64), nullable=False)
    graph_settings = Column(Text, nullable=False)
    created = Column(DateTime, nullable=False)
    updated = Column(DateTime, nullable=False)
    status = Column(Integer, default=0)

class DbKgModels(Base):
    __tablename__ = "kg_models"
    
    id = Column(Integer, primary_key=True, index=True)
    model_category = Column(String(64), nullable=False)
    model_name = Column(String(64), nullable=False)
    model_description = Column(String(64), nullable=False)
    model_settings = Column(Text, nullable=False)
    created = Column(DateTime, nullable=False)
    updated = Column(DateTime, nullable=False)
    status = Column(Integer, default=0)
    
class DbKgTask(Base):
    __tablename__ = "kg_tasks"
    
    id = Column(Integer, primary_key=True, index=True)
    proj_id = Column(Integer, ForeignKey('kg_projs.id'), nullable=False)
    task_category = Column(String(64), nullable=False)
    task_name = Column(String(32), nullable=False)
    task_content = Column(Text, nullable=False)
    task_log = Column(Text, nullable=True)
    created = Column(DateTime, nullable=False)
    updated = Column(DateTime, nullable=False)
    status = Column(Integer, default=0)
    
class DbKgSubGraph(Base):    
    __tablename__ = "kg_sub_graph"
    id = Column(Integer, primary_key=True, index=True)
    graph_name = Column(String(64), nullable=False)
    graph_content = Column(Text)
    status = Column(Integer, default=0)

class DbKgNode(Base):
    __tablename__ = "kg_nodes"
    
    id = Column(Integer, primary_key=True, index=True)
    graph_id = Column(Integer, default=0)
    name = Column(String(64), nullable=False)
    category = Column(String(64), nullable=False)
    layout = Column(String(100))
    version = Column(String(16))
    status = Column(Integer, default=0)

    props = relationship("DbKgProp", cascade = "delete", back_populates="node" ,foreign_keys='DbKgProp.ref_id', primaryjoin="DbKgNode.id==DbKgProp.ref_id")

    
class DbKgEdge(Base):
    __tablename__ = "kg_edges"
    
    id = Column(Integer, primary_key=True, index=True)
    graph_id = Column(Integer, default=0)
    category = Column(String(64), nullable=False)
    src_id = Column(Integer, ForeignKey('kg_nodes.id'), nullable=False)
    dest_id = Column(Integer, ForeignKey('kg_nodes.id'), nullable=False)
    name = Column(String(64), nullable=False)
    version = Column(String(16))
    status = Column(Integer, default=0)

    src_node = relationship("DbKgNode",  primaryjoin="DbKgEdge.src_id==DbKgNode.id", lazy="select")
    dest_node = relationship("DbKgNode",  primaryjoin="DbKgEdge.dest_id==DbKgNode.id", lazy="select")

class DbKgProp(Base):
    __tablename__ = "kg_props"
    
    id = Column(Integer, primary_key=True, index=True)
    category = Column(Integer, default=0)
    ref_id = Column(Integer, ForeignKey('kg_nodes.id'), nullable=False)
    prop_name = Column(String(64), nullable=False)
    prop_title = Column(String(64), nullable=False)
    prop_value = Column(Text)

    node = relationship("DbKgNode", back_populates="props", foreign_keys=[ref_id], primaryjoin="DbKgProp.ref_id==DbKgNode.id")
    #edge = relationship("DbKgEdge", back_populates="props", foreign_keys=[ref_id], primaryjoin="DbKgProp.ref_id==DbKgEdge.id")

class DbDictICD(Base):
    __tablename__ = "dict_icd10"
    
    id = Column(Integer, primary_key=True, index=True)
    icd_code = Column(String(50), nullable=False)
    icd_name = Column(String(150), nullable=False)
    
class DbDictDRG(Base):
    __tablename__ = "dict_drg"
    
    id = Column(Integer, primary_key=True, index=True)
    drg_code = Column(String(64), nullable=False)
    drg_name = Column(String(128), nullable=False)
    drg_weight = Column(Float(128), nullable=False)
    

class DbDictDrug(Base):
    __tablename__ = "dict_social_drug"
    
    id = Column(Integer, primary_key=True, index=True)
    data_source = Column(String(10), nullable=True)
    drug_code = Column(String(64), nullable=True)
    reg_name = Column(String(64), nullable=True)
    prod_name = Column(String(128), nullable=True)
    reg_dosage_form = Column(String(50), nullable=True)
    act_dosage_form = Column(String(50), nullable=True)
    reg_spec = Column(String(250), nullable=True)
    act_spec = Column(String(250), nullable=True)
    pkg_mat = Column(String(150), nullable=True)
    min_pack_size = Column(String(50), nullable=True)
    min_pack_unit = Column(String(100), nullable=True)
    min_dosage_unit = Column(String(100), nullable=True)
    prod_factory = Column(String(250), nullable=True)
    license_no = Column(String(64), nullable=True)
    drug_std_code = Column(String(64), nullable=True)
    subpkg_factory = Column(String(64), nullable=True)
    sales_status = Column(String(100), nullable=True)
    social_insurance_name = Column(String(200), nullable=True)
    jiayi_category = Column(String(50), nullable=True)
    social_dosage_form = Column(String(50), nullable=True)
    serial_no = Column(String(50), nullable=True)
    comments = Column(String(300), nullable=True)


class DbKgSchemas(Base):
    __tablename__ = "kg_schemas"
    
    id = Column(Integer, primary_key=True, index=True)
    schema_system = Column(String(64), nullable=False)
    schema_type = Column(String(64), nullable=False)
    name = Column(String(64), nullable=False)
    category = Column(String(64), nullable=False)
    content = Column(Text())
    version = Column(String(16))
    status = Column(Integer, default=0)
    
class DbUsers(Base):
    __tablename__ ="users"
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(32), nullable=False)
    full_name = Column(String(64), nullable=False)
    email = Column(String(100), nullable=False)
    hashed_password = Column(String(64), nullable=False)
    status = Column(Integer, default=0)

class DbKgDataset(Base):
    __tablename__ = "kg_datasets"
    id = Column(Integer, primary_key=True, index=True)
    data_category = Column(String(64), nullable=False)
    data_name = Column(String(64), nullable=False)
    data_comments = Column(Text(), nullable=False)
    created = Column(DateTime, nullable=False)
    updated = Column(DateTime, nullable=False)
    status = Column(Integer, default=0)

class DbUserDataRelation(Base):
    __tablename__ = "user_data_relations"
    
    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
    data_category = Column(String(255), nullable=False)  # 表名
    data_id = Column(Integer, nullable=False)  # 表中数据ID
    role_id = Column(Integer, nullable=False)  # 用户角色ID
    created = Column(DateTime, nullable=False)
    updated = Column(DateTime, nullable=False)
    user_name = Column(String(64), nullable=False)
    role_name = Column(String(64), nullable=False)
    
    user = relationship("DbUsers", back_populates="data_relations")

# 在DbUsers中添加反向关系
DbUsers.data_relations = relationship("DbUserDataRelation", back_populates="user")

__all__=['DbKgEdge','DbKgNode','DbKgProp','DbDictICD','DbDictDRG',
         'DbDictDrug','DbKgSchemas','DbKgSubGraph','DbKgModels',
         'DbKgGraphs', 'DbKgDataset', 'DbUserDataRelation']