#!/bin/bash

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

start() {
    echo -e "${BLUE}🚀 启动智能星球计划...${NC}"

    echo -e "${BLUE}🗄️  启动数据库...${NC}"
    docker start planet_postgres planet_redis 2>/dev/null || docker-compose up -d postgres redis

    sleep 3

    echo -e "${BLUE}🔧 启动后端...${NC}"
    pkill -f "uvicorn" 2>/dev/null || true
    cd "$SCRIPT_DIR/backend"
    PYTHONPATH="$SCRIPT_DIR/backend" nohup python3 -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload > /tmp/planet_backend.log 2>&1 &
    BACKEND_PID=$!

    echo "    等待后端启动..."
    for i in {1..10}; do
        sleep 2
        if curl -s http://localhost:8000/health > /dev/null 2>&1; then
            echo -e "    ${GREEN}✅ 后端已就绪${NC}"
            break
        fi
        if [ $i -eq 10 ]; then
            echo -e "${RED}❌ 后端启动失败${NC}"
            tail -10 /tmp/planet_backend.log
            exit 1
        fi
    done

    echo -e "${BLUE}🌐 启动前端...${NC}"
    pkill -f "vite" 2>/dev/null || true
    pkill -f "bun run dev" 2>/dev/null || true
    cd "$SCRIPT_DIR/frontend"
    nohup bun run dev --port 3000 > /tmp/planet_frontend.log 2>&1 &

    sleep 3

    echo ""
    echo -e "${GREEN}✅ 启动完成!${NC}"
    echo "   前端: http://localhost:3000"
    echo "   后端: http://localhost:8000"
}

stop() {
    echo -e "${YELLOW}🛑 停止服务...${NC}"
    pkill -f "uvicorn" 2>/dev/null || true
    pkill -f "vite" 2>/dev/null || true
    pkill -f "bun run dev" 2>/dev/null || true
    docker stop planet_postgres planet_redis 2>/dev/null || true
    echo -e "${GREEN}✅ 已停止${NC}"
}

restart() {
    stop
    sleep 1
    start
}

health() {
    echo "📊 容器状态:"
    docker ps --filter "name=planet_" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

    echo ""
    echo "🔍 服务状态:"
    if curl -s http://localhost:8000/health > /dev/null 2>&1; then
        echo -e "  后端: ${GREEN}✅ 运行中${NC}"
    else
        echo -e "  后端: ${RED}❌ 未运行${NC}"
    fi

    if curl -s http://localhost:3000 > /dev/null 2>&1; then
        echo -e "  前端: ${GREEN}✅ 运行中${NC}"
    else
        echo -e "  前端: ${RED}❌ 未运行${NC}"
    fi
}

log() {
    case "$1" in
        -f|--frontend)
            echo "📝 前端日志 (Ctrl+C 退出):"
            tail -f /tmp/planet_frontend.log
            ;;
        -b|--backend)
            echo "📝 后端日志 (Ctrl+C 退出):"
            tail -f /tmp/planet_backend.log
            ;;
        *)
            echo "📝 最近日志:"
            echo "--- 后端 ---"
            tail -20 /tmp/planet_backend.log 2>/dev/null || echo "无日志"
            echo "--- 前端 ---"
            tail -20 /tmp/planet_frontend.log 2>/dev/null || echo "无日志"
            ;;
    esac
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    health)
        health
        ;;
    log)
        log "$2"
        ;;
    *)
        echo "用法: ./planet.sh {start|stop|restart|health|log}"
        echo ""
        echo "命令:"
        echo "  start      启动服务"
        echo "  stop       停止服务"
        echo "  restart    重启服务"
        echo "  health     检查健康状态"
        echo "  log        查看日志"
        echo "  log -f     查看前端日志"
        echo "  log -b     查看后端日志"
        ;;
esac
