import json
from datetime import datetime
from typing import Optional

from pydantic import BaseModel
from sqlalchemy import Column, Integer, String, BigInteger, ForeignKey, DateTime, Text, JSON
from sqlalchemy.orm import relationship, backref

from app.models.base_model import Base

class DialogModel(Base):
    __tablename__ = 'dialogs'
    __mapper_args__ = {
        # "order_by": 'SEQ'
    }
    id = Column(String(36), primary_key=True)  #  id
    create_date = Column(DateTime, default=datetime.now())             # 创建时间
    update_date = Column(DateTime, default=datetime.now(), onupdate=datetime.now())             # 更新时间
    tenant_id = Column(String(36))              # 创建人
    name = Column(String(255))                 # 名称
    description = Column(Text)                 # 说明
    icon = Column(Text, default="intelligentFrame1")                         # 图标
    status = Column(String(1), default="0")                 # 状态
    dialog_type = Column(String(1))            #  平台
    # agent_id = Column(String(36))
    mode = Column(String(36))
    parameters = Column(Text)
    kb_ids = Column(String(128))

    def get_id(self):
        return str(self.id)

    def to_json(self):
        return {
            'id': self.id,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S'),
            'update_date': self.update_date.strftime('%Y-%m-%d %H:%M:%S'),
            'user_id': self.tenant_id,
            'name': self.name,
            'description': self.description,
            'icon': self.icon,
            'status': self.status,
            'agentType': self.dialog_type,
            # 'agentId': self.agent_id,
            'mode': self.mode,
        }

    def get_kb_ids(self):
        return json.loads(self.kb_ids) if self.kb_ids else []


class ConversationModel(Base):
    __tablename__ = 'conversation'
    id = Column(String(32), primary_key=True)  #  id
    create_date = Column(DateTime)             # 创建时间
    create_time = Column(BigInteger)
    update_date = Column(DateTime)             # 更新时间
    update_time = Column(BigInteger, index=True)
    tenant_id = Column(Integer)              # 创建人
    dialog_id = Column(String(32))
    name = Column(String(255))                 # 名称
    message = Column(JSON)                 # 说明
    reference = Column(JSON)                         # 图标

    def get_id(self):
        return str(self.id)


    def to_json(self):
        return {
            'id': self.id,
            'updated_time': self.update_time,
            'update_date': datetime.fromtimestamp(self.update_time / 1000.0).strftime('%Y-%m-%d %H:%M:%S'),
            'name': self.name,
        }


class dialogData(BaseModel):
    id: str
    name: str
    description: Optional[str] = ""
    dialogType: str
    icon: str
    mode: str


class dialogDataUpdate(BaseModel):
    id: str
    status: Optional[str] = "1"
    icon: Optional[str] = ""
    name: Optional[str] = ""
    description: Optional[str] = None


class dialogList(BaseModel):
    current: Optional[int] = 1
    pageSize: Optional[int] = 10
    keyword: Optional[str] = ""
    label: Optional[list] = []
    status: Optional[str] = ""
    mode: Optional[str] = ""