#!/bin/bash
#
# POUNCE Deployment Script
# Usage: ./deploy.sh [dev|prod]
#

set -e

MODE=${1:-dev}
echo "================================================"
echo "  POUNCE Deployment - Mode: $MODE"
echo "================================================"
echo ""

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Functions
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

# ============================================
# 1. Check prerequisites
# ============================================
log_info "Checking prerequisites..."

if ! command -v python3 &> /dev/null; then
    log_error "Python3 not found. Please install Python 3.10+"
    exit 1
fi

if ! command -v node &> /dev/null; then
    log_error "Node.js not found. Please install Node.js 18+"
    exit 1
fi

if ! command -v npm &> /dev/null; then
    log_error "npm not found. Please install npm"
    exit 1
fi

log_info "Prerequisites OK!"
echo ""

# ============================================
# 2. Setup Backend
# ============================================
log_info "Setting up Backend..."

cd backend

# Create virtual environment if not exists
if [ ! -d "venv" ]; then
    log_info "Creating Python virtual environment..."
    python3 -m venv venv
fi

# Activate venv
source venv/bin/activate

# Install dependencies
log_info "Installing Python dependencies..."
pip install -q --upgrade pip
pip install -q -r requirements.txt

# Create .env if not exists
if [ ! -f ".env" ]; then
    log_warn ".env file not found, copying from env.example..."
    if [ -f "env.example" ]; then
        cp env.example .env
        log_warn "Please edit backend/.env with your settings!"
    else
        log_error "env.example not found!"
    fi
fi

# Initialize database
log_info "Initializing database..."
python scripts/init_db.py

cd ..
log_info "Backend setup complete!"
echo ""

# ============================================
# 3. Setup Frontend
# ============================================
log_info "Setting up Frontend..."

cd frontend

# Install dependencies
log_info "Installing npm dependencies..."
npm install --silent

# Create .env.local if not exists
if [ ! -f ".env.local" ]; then
    log_warn ".env.local not found, creating..."
    echo "NEXT_PUBLIC_API_URL=http://localhost:8000" > .env.local
fi

# Build for production
if [ "$MODE" == "prod" ]; then
    log_info "Building for production..."
    npm run build
fi

cd ..
log_info "Frontend setup complete!"
echo ""

# ============================================
# 4. Start Services
# ============================================
if [ "$MODE" == "dev" ]; then
    echo ""
    echo "================================================"
    echo "  Development Setup Complete!"
    echo "================================================"
    echo ""
    echo "To start the services:"
    echo ""
    echo "  Backend (Terminal 1):"
    echo "    cd backend && source venv/bin/activate"
    echo "    uvicorn app.main:app --reload --host 0.0.0.0 --port 8000"
    echo ""
    echo "  Frontend (Terminal 2):"
    echo "    cd frontend && npm run dev"
    echo ""
    echo "Then open: http://localhost:3000"
    echo ""
else
    echo ""
    echo "================================================"
    echo "  Production Setup Complete!"
    echo "================================================"
    echo ""
    echo "To start with PM2:"
    echo ""
    echo "  # Backend"
    echo "  cd backend && source venv/bin/activate"
    echo "  pm2 start 'uvicorn app.main:app --host 0.0.0.0 --port 8000' --name pounce-backend"
    echo ""
    echo "  # Frontend"
    echo "  cd frontend"
    echo "  pm2 start 'npm start' --name pounce-frontend"
    echo ""
    echo "Or use Docker:"
    echo "  docker-compose up -d"
    echo ""
fi

