#!/bin/bash
# 通过 Homebrew 安装并启动 MySQL（macOS）
# 请在终端中执行：./scripts/install_mysql.sh

set -e
cd "$(dirname "$0")/.."

# 查找 brew
BREW=""
for p in /opt/homebrew/bin/brew /usr/local/bin/brew; do
  [ -x "$p" ] && BREW="$p" && break
done
if [ -z "$BREW" ]; then
  echo ">>> 未找到 Homebrew。请先安装："
  echo "    /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
  echo "    安装后按提示把 brew 加入 PATH"
  exit 1
fi

echo ">>> 安装 MySQL..."
$BREW install mysql

echo ">>> 启动 MySQL 服务..."
$BREW services start mysql

echo ""
echo ">>> MySQL 安装并已启动。等待几秒后，创建库和用户请执行："
echo "    ./scripts/setup_mysql_local.sh"
echo ""
echo ">>> 然后在 .env 中设置："
echo "    DATABASE_URL=mysql+aiomysql://root:yingping@localhost:3306/allm?charset=utf8mb4"
