#!/bin/bash
# SQL Agent 数据库连接辅助脚本 (via docker exec)
# 用法:
#   ./sql-helper.sh psql [SQL]        # PostgreSQL 交互/查询
#   ./sql-helper.sh mysql [SQL]       # MySQL 交互/查询
#   ./sql-helper.sh schema-pg         # PostgreSQL 所有表
#   ./sql-helper.sh schema-mysql      # MySQL 所有表
#   ./sql-helper.sh analyze-pg        # PostgreSQL 索引分析
#   ./sql-helper.sh analyze-mysql     # MySQL 查询分析

set -euo pipefail

# 密码从 .env 文件通过环境变量传入
# PGPASSWORD 和 MYSQL_PWD_SQL 需在调用前已 export

case "${1:-help}" in
  psql)
    shift 2>/dev/null || true
    if [ $# -ge 1 ]; then
      docker exec -e PGPASSWORD="$PGPASSWORD" postgres psql -U openclaw -d openclaw -c "$1" -t 2>&1
    else
      docker exec -e PGPASSWORD="$PGPASSWORD" -it postgres psql -U openclaw -d openclaw 2>&1
    fi
    ;;
  mysql)
    shift 2>/dev/null || true
    if [ $# -ge 1 ]; then
      docker exec mysql mysql -u openclaw -p"$MYSQL_PWD_SQL" openclaw -e "$1" -t 2>&1
    else
      docker exec -it mysql mysql -u openclaw -p"$MYSQL_PWD_SQL" openclaw 2>&1
    fi
    ;;
  schema-pg)
    echo "=== PostgreSQL 表列表 ==="
    docker exec -e PGPASSWORD="$PGPASSWORD" postgres psql -U openclaw -d openclaw -c "
      SELECT table_schema, table_name, table_type
      FROM information_schema.tables
      WHERE table_schema NOT IN ('pg_catalog','information_schema')
      ORDER BY table_schema, table_name;
    " -t 2>&1
    ;;
  schema-mysql)
    echo "=== MySQL 表列表 ==="
    docker exec mysql mysql -u openclaw -p"$MYSQL_PWD_SQL" openclaw -e "
      SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
      FROM information_schema.tables
      WHERE TABLE_SCHEMA = 'openclaw'
      ORDER BY TABLE_NAME;
    " -t 2>&1
    ;;
  analyze-pg)
    echo "=== PostgreSQL 索引使用分析 ==="
    docker exec -e PGPASSWORD="$PGPASSWORD" postgres psql -U openclaw -d openclaw -c "
      SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
      FROM pg_stat_user_indexes
      ORDER BY idx_scan DESC LIMIT 20;
    " -t 2>&1
    ;;
  analyze-mysql)
    echo "=== MySQL EXPLAIN 经典语句 ==="
    docker exec mysql mysql -u openclaw -p"$MYSQL_PWD_SQL" openclaw -e "
      SELECT DIGEST_TEXT, COUNT_STAR, SUM_ROWS_EXAMINED, SUM_NO_INDEX_USED
      FROM performance_schema.events_statements_summary_by_digest
      WHERE SUM_NO_INDEX_USED > 0
      ORDER BY COUNT_STAR DESC LIMIT 10;
    " -t 2>&1 || echo "performance_schema 可能需要额外配置"
    ;;
  test)
    echo "=== PostgreSQL 连接测试 ==="
    docker exec -e PGPASSWORD="$PGPASSWORD" postgres psql -U openclaw -d openclaw -c "SELECT current_database() AS db, version();" -t 2>&1
    echo ""
    echo "=== MySQL 连接测试 ==="
    docker exec mysql mysql -u openclaw -p"$MYSQL_PWD_SQL" openclaw -e "SELECT database() AS db, version();" -t 2>&1
    ;;
  *)
    echo "SQL Agent 数据库辅助脚本 (via docker exec)"
    echo ""
    echo "用法:"
    echo "  ./sql-helper.sh psql [SQL]       # PostgreSQL 交互/查询"
    echo "  ./sql-helper.sh mysql [SQL]      # MySQL 交互/查询"
    echo "  ./sql-helper.sh schema-pg        # PostgreSQL 所有表"
    echo "  ./sql-helper.sh schema-mysql     # MySQL 所有表"
    echo "  ./sql-helper.sh analyze-pg       # PostgreSQL 索引分析"
    echo "  ./sql-helper.sh analyze-mysql    # MySQL 查询分析"
    echo "  ./sql-helper.sh test             # 测试连接"
    ;;
esac
