#!/usr/bin/env bash
# CodeSpectra 一键启动脚本
# 构建前端并启动后端服务
# 用法: ./start.sh <notifier_user_name> <notifier_token>

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

if [ $# -lt 2 ]; then
    echo "用法: $0 <notifier_user_name> <notifier_token>"
    echo "  notifier_user_name  notifier 账户名"
    echo "  notifier_token      notifier access_token"
    exit 1
fi

# 1. 构建前端
echo "[start] building frontend..."
cd frontend && npm install --silent && npx vite build && cd "$SCRIPT_DIR"

# 2. 启动后端
HOST="${CODESPECTRA_HOST:-0.0.0.0}"
PORT="${CODESPECTRA_PORT:-8000}"

export PYTHONPATH="$SCRIPT_DIR:$PYTHONPATH"

echo "[start] starting server @ ${HOST}:${PORT}"
exec .venv/bin/python -m backend.server --host "$HOST" --port "$PORT" \
    --notifier-name "$1" --notifier-token "$2"