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

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

echo "== 后端编译检查 =="
cd "$ROOT_DIR/apps/backend"
./.venv/bin/python -m compileall app

echo "== 后端 OpenAPI 检查 =="
DATABASE_URL=sqlite:///:memory: ./.venv/bin/python - <<'PY'
from app.main import app
paths = app.openapi().get("paths", {})
print(f"OpenAPI paths: {len(paths)}")
assert len(paths) >= 32
PY

echo "== Alembic MySQL 静态 SQL 检查 =="
./.venv/bin/alembic upgrade head --sql >/tmp/ai_kb_v2_migration.sql
wc -l /tmp/ai_kb_v2_migration.sql

echo "== 用户端 H5 构建 =="
cd "$ROOT_DIR/apps/user-client"
npm run build

echo "== 管理后台构建 =="
cd "$ROOT_DIR/apps/admin-web"
npm run build

echo "== Docker Compose 配置检查 =="
cd "$ROOT_DIR"
docker compose -f docker-compose.dev.yml config --quiet

echo "阶段六自动验证完成"
