import sqlalchemy

from .base import Base


class MonitoringMessage(Base):
    """Monitoring message records"""

    __tablename__ = 'monitoring_messages'

    id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        nullable=False,
    )
    timestamp = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    bot_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    bot_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    pipeline_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    message_content = sqlalchemy.Column(sqlalchemy.Text, nullable=False)
    session_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    status = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)  # success, error, pending
    level = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)  # info, warning, error, debug
    platform = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    user_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    user_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)  # User display name
    runner_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)  # Runner name for this query
    variables = sqlalchemy.Column(sqlalchemy.Text, nullable=True)  # Query variables as JSON string
    role = sqlalchemy.Column(sqlalchemy.String(50), nullable=True, default='user')  # user, assistant

    __table_args__ = (
        sqlalchemy.Index('ix_monitoring_messages_workspace_timestamp', 'workspace_uuid', 'timestamp'),
        sqlalchemy.Index('ix_monitoring_messages_workspace_session', 'workspace_uuid', 'session_id'),
    )


class MonitoringLLMCall(Base):
    """LLM call records"""

    __tablename__ = 'monitoring_llm_calls'

    id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        nullable=False,
    )
    timestamp = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    model_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    input_tokens = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
    output_tokens = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
    total_tokens = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
    duration = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)  # milliseconds
    cost = sqlalchemy.Column(sqlalchemy.Float, nullable=True)
    status = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)  # success, error
    bot_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    bot_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    pipeline_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    session_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    error_message = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
    message_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)  # Associated message ID

    __table_args__ = (
        sqlalchemy.Index('ix_monitoring_llm_calls_workspace_timestamp', 'workspace_uuid', 'timestamp'),
        sqlalchemy.Index('ix_monitoring_llm_calls_workspace_session', 'workspace_uuid', 'session_id'),
    )


class MonitoringToolCall(Base):
    """Tool call records"""

    __tablename__ = 'monitoring_tool_calls'

    id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        nullable=False,
    )
    timestamp = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    tool_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    tool_source = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)  # native, plugin, mcp, skill
    duration = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)  # milliseconds
    status = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)  # success, error
    bot_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    bot_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    pipeline_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    session_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    message_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    arguments = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
    result = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
    error_message = sqlalchemy.Column(sqlalchemy.Text, nullable=True)

    __table_args__ = (
        sqlalchemy.Index('ix_monitoring_tool_calls_workspace_timestamp', 'workspace_uuid', 'timestamp'),
        sqlalchemy.Index('ix_monitoring_tool_calls_workspace_session', 'workspace_uuid', 'session_id'),
    )


class MonitoringSession(Base):
    """Session tracking records"""

    __tablename__ = 'monitoring_sessions'

    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        primary_key=True,
    )
    session_id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    bot_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    bot_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    pipeline_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    message_count = sqlalchemy.Column(sqlalchemy.Integer, nullable=False, default=0)
    start_time = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    last_activity = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    is_active = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False, default=True, index=True)
    platform = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    user_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    user_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)  # User display name

    __table_args__ = (
        sqlalchemy.Index('ix_monitoring_sessions_workspace_activity', 'workspace_uuid', 'last_activity'),
        sqlalchemy.Index('ix_monitoring_sessions_workspace_active', 'workspace_uuid', 'is_active'),
    )


class MonitoringError(Base):
    """Error log records"""

    __tablename__ = 'monitoring_errors'

    id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        nullable=False,
    )
    timestamp = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    error_type = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    error_message = sqlalchemy.Column(sqlalchemy.Text, nullable=False)
    bot_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    bot_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    pipeline_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    session_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    stack_trace = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
    message_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)  # Associated message ID

    __table_args__ = (
        sqlalchemy.Index('ix_monitoring_errors_workspace_timestamp', 'workspace_uuid', 'timestamp'),
        sqlalchemy.Index('ix_monitoring_errors_workspace_session', 'workspace_uuid', 'session_id'),
    )


class MonitoringEmbeddingCall(Base):
    """Embedding call records"""

    __tablename__ = 'monitoring_embedding_calls'

    id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        nullable=False,
    )
    timestamp = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    model_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
    prompt_tokens = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
    total_tokens = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)
    duration = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)  # milliseconds
    input_count = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)  # Number of input texts
    status = sqlalchemy.Column(sqlalchemy.String(50), nullable=False)  # success, error
    error_message = sqlalchemy.Column(sqlalchemy.Text, nullable=True)
    # Optional context fields
    knowledge_base_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    query_text = sqlalchemy.Column(sqlalchemy.Text, nullable=True)  # For retrieval calls
    session_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    message_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    call_type = sqlalchemy.Column(sqlalchemy.String(50), nullable=True)  # embedding, retrieve

    __table_args__ = (
        sqlalchemy.Index(
            'ix_monitoring_embedding_calls_workspace_timestamp',
            'workspace_uuid',
            'timestamp',
        ),
        sqlalchemy.Index(
            'ix_monitoring_embedding_calls_workspace_kb',
            'workspace_uuid',
            'knowledge_base_id',
        ),
    )


class MonitoringFeedback(Base):
    """User feedback records (like/dislike) from AI Bot conversations"""

    __tablename__ = 'monitoring_feedback'

    id = sqlalchemy.Column(sqlalchemy.String(255), primary_key=True)
    workspace_uuid = sqlalchemy.Column(
        sqlalchemy.String(36),
        sqlalchemy.ForeignKey('workspaces.uuid', ondelete='CASCADE'),
        nullable=False,
    )
    timestamp = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, index=True)
    feedback_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=False, index=True)
    feedback_type = sqlalchemy.Column(sqlalchemy.Integer, nullable=False)  # 1=like, 2=dislike
    feedback_content = sqlalchemy.Column(sqlalchemy.Text, nullable=True)  # User feedback text
    inaccurate_reasons = sqlalchemy.Column(sqlalchemy.Text, nullable=True)  # JSON list of inaccurate reasons
    # Context fields
    bot_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    bot_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    pipeline_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    session_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    message_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    stream_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True, index=True)
    user_id = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
    platform = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)  # e.g., wecom

    __table_args__ = (
        sqlalchemy.UniqueConstraint(
            'workspace_uuid',
            'feedback_id',
            name='uq_monitoring_feedback_workspace_feedback_id',
        ),
        sqlalchemy.Index('ix_monitoring_feedback_workspace_timestamp', 'workspace_uuid', 'timestamp'),
        sqlalchemy.Index('ix_monitoring_feedback_workspace_session', 'workspace_uuid', 'session_id'),
    )
