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

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
project_root="$(cd "$script_dir/.." && pwd)"
expected="RESET insurance_s1"
confirmation="${1:-}"

echo "即将重建数据库：insurance_s1_core、insurance_s1_agent、insurance_s1_analytics"
echo "即将删除 Redis 前缀：ins:s1:*"
if [[ -z "${confirmation//[[:space:]]/}" ]]; then
    read -r -p "请输入 $expected 继续: " confirmation
fi
[[ "$confirmation" == "$expected" ]] || { echo "确认文本不匹配，已取消。" >&2; exit 1; }

cd "$project_root/backend"
uv run alembic -c alembic.ini --name alembic_analytics downgrade base
uv run alembic -c alembic.ini --name alembic_agent downgrade base
uv run alembic -c alembic.ini --name alembic_core downgrade base
uv run python -m zbt.commands.reset_cache
"$script_dir/migrate.sh"
"$script_dir/seed.sh"
"$script_dir/build-analytics.sh"
