# app/models/financial_information_template.py
from sqlalchemy import Column, Integer, String, Text, Boolean, ForeignKey, JSON
from utils.mysql_db import Base
from mixins.timestamp_mixin import TimestampMixin
from uuid import uuid4
from datetime import datetime
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func


class ChatMessage(Base, TimestampMixin):
    __tablename__ = "chat_messages"

    id = Column(String(36), primary_key=True, index=True, default=lambda: str(uuid4()))
    socket_id = Column(String(255), nullable=True)
    session_id = Column(
        String(255),
        ForeignKey("chat_sessions.session_id", ondelete="CASCADE"),
        nullable=False,
        index=True,
    )
    question = Column(Text, nullable=True)
    response = Column(Text, nullable=True)
    tokens_used = Column(Integer, default=0)
    ai_model_used = Column(String(25), nullable=True)
    is_active = Column(Boolean, default=True)
    session = relationship("ChatSession", back_populates="messages")

    def update_modify(self, user_id):
        """Update the modified date"""
        self.modified_at = func.now()
        self.modified_by = user_id

    def to_dict(self):
        "Convert the class to a dictionary"
        return {
            "id": self.id,
            "question": self.question,
            "response": self.response,
            "tokens_used": self.tokens_used,
            "ai_model_used": self.ai_model_used,
            "created_at": self.created_at,
            "modified_at": self.modified_at,
            "created_by": self.created_by,
            "modified_by": self.modified_by,
        }


class ChatSession(Base, TimestampMixin):
    __tablename__ = "chat_sessions"

    id = Column(Integer, primary_key=True, autoincrement=True)
    session_id = Column(
        String(255),
        unique=True,
        nullable=False,
        index=True,
        default=lambda: str(uuid4()),
    )
    name = Column(String(255), nullable=True)
    project_id = Column(String(255), nullable=False)
    is_active = Column(Boolean, default=True)
    team_id = Column(String(50), nullable=False)
    selected_files = Column(JSON, nullable=True, default=list)
    messages = relationship(
        "ChatMessage", back_populates="session", cascade="all, delete-orphan"
    )

    def update_modify(self, user_id):
        """Update the modified date"""
        self.modified_at = func.now()
        self.modified_by = user_id

    def to_dict(self):
        "Convert the class to a dictionary"
        return {
            "id": self.id,
            "session_id": self.session_id,
            "name": self.name,
            "project_id": self.project_id,
            "is_active": self.is_active,
            "team_id": self.team_id,
            "created_at": self.created_at,
            "modified_at": self.modified_at,
            "created_by": self.created_by,
            "modified_by": self.modified_by,
            "selected_files": self.selected_files,
        }
