"""SQL query strings for the Devin CLI sessions and message_nodes tables."""

LIST_SESSIONS = (
    "SELECT id, title, working_directory, last_activity_at, created_at, hidden "
    "FROM sessions ORDER BY last_activity_at DESC LIMIT ?"
)

COUNT_MESSAGES = "SELECT COUNT(*) FROM message_nodes WHERE session_id = ?"

GET_SESSION = (
    "SELECT id, title, working_directory, last_activity_at, created_at, "
    "model, backend_type, agent_mode, metadata "
    "FROM sessions WHERE id = ?"
)

GET_MESSAGE_NODES = (
    "SELECT row_id, parent_node_id, chat_message, created_at "
    "FROM message_nodes WHERE session_id = ? ORDER BY created_at ASC"
)

DETECT_BY_CWD = (
    "SELECT id FROM sessions WHERE working_directory = ? ORDER BY last_activity_at DESC LIMIT 1"
)

DETECT_MOST_RECENT = "SELECT id FROM sessions ORDER BY last_activity_at DESC LIMIT 1"

# Schema version detection — Devin uses refinery_schema_history.
SCHEMA_VERSION = "SELECT MAX(version) FROM refinery_schema_history"

# Minimum supported schema migration version.
# Migration 5 (message_forest) introduced the row_id/node_id/parent_node_id model.
MINIMUM_SCHEMA_VERSION = 5
