#!/usr/bin/env bash
set -euo pipefail

project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
backend_root="$project_root/backend"

[[ -f "$backend_root/.env" ]] || { echo "缺少 backend/.env。请先创建该文件并配置后端环境变量。" >&2; exit 1; }
command -v uv >/dev/null 2>&1 || { echo "未找到 uv。" >&2; exit 1; }

cd "$project_root"
uv run --project backend python -c 'import sys; assert sys.version_info[:2] == (3, 12); print(sys.version)'

command -v nc >/dev/null 2>&1 || { echo "未找到 nc，无法检查 MySQL 和 Redis 端口。" >&2; exit 1; }
nc -z 127.0.0.1 3306 || { echo "MySQL 127.0.0.1:3306 未监听。" >&2; exit 1; }
nc -z 127.0.0.1 6379 || { echo "Redis 127.0.0.1:6379 未监听。" >&2; exit 1; }

echo "环境检查通过：uv、Python 3.12、MySQL、Redis。"
