#!/bin/bash

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

# Function to kill background processes on exit
cleanup() {
    echo -e "\n${YELLOW}🛑 Stopping services...${NC}"
    kill $(jobs -p) 2>/dev/null
    exit
}

trap cleanup SIGINT SIGTERM

# Header
clear
echo -e "${CYAN}"
echo "╔══════════════════════════════════════════════╗"
echo "║         🐴 GoHorse Jobs - Dev Server         ║"
echo "╚══════════════════════════════════════════════╝"
echo -e "${NC}"

# Interactive menu
echo -e "${GREEN}Select an option:${NC}\n"
echo -e "  ${YELLOW}1)${NC} 🚀 Start (Frontend + Backend)"
echo -e "  ${YELLOW}2)${NC} 🌱 Start with Seed (Reset DB + Seed + Start)"
echo -e "  ${YELLOW}3)${NC} 📦 Start All (Frontend + Backend + Backoffice)"
echo -e "  ${YELLOW}4)${NC} 🧪 Run Tests (Backend E2E)"
echo -e "  ${YELLOW}5)${NC} 🌿 Seed Only (No restart)"
echo -e "  ${YELLOW}0)${NC} ❌ Exit"
echo ""
read -p "Enter option [1-5, 0]: " choice

case $choice in
    1)
        echo -e "\n${GREEN}🚀 Starting Development Environment...${NC}\n"
        
        # Backend
        echo -e "${BLUE}🔹 Checking Backend...${NC}"
        cd backend && go mod tidy
        if command -v swag &> /dev/null; then
            echo -e "${BLUE}🔹 Generating Swagger Docs...${NC}"
            swag init -g cmd/api/main.go --parseDependency --parseInternal 2>/dev/null
        fi
        cd ..
        
        # Frontend deps
        if [ ! -d "frontend/node_modules" ]; then
            echo -e "${BLUE}🔹 Installing Frontend Dependencies...${NC}"
            cd frontend && npm install && cd ..
        fi
        
        # Start services
        echo -e "${BLUE}🔹 Starting Backend on port 8521...${NC}"
        (cd backend && go run cmd/api/main.go) &
        BACKEND_PID=$!
        sleep 2
        
        echo -e "${BLUE}🔹 Starting Frontend on port 8963...${NC}"
        (cd frontend && npm run dev -- -p 8963) &
        FRONTEND_PID=$!
        
        echo -e "\n${GREEN}✅ Services running:${NC}"
        echo -e "   ${CYAN}Backend:${NC}  http://localhost:8521"
        echo -e "   ${CYAN}Frontend:${NC} http://localhost:8963"
        echo -e "   ${CYAN}Swagger:${NC}  http://localhost:8521/swagger/index.html"
        echo ""
        
        wait $BACKEND_PID $FRONTEND_PID
        ;;
        
    2)
        echo -e "\n${GREEN}🌱 Starting with Database Reset & Seed...${NC}\n"
        
        # Backend prep
        echo -e "${BLUE}🔹 Checking Backend...${NC}"
        cd backend && go mod tidy
        if command -v swag &> /dev/null; then
            swag init -g cmd/api/main.go --parseDependency --parseInternal 2>/dev/null
        fi
        cd ..
        
        # Seeder deps
        cd seeder-api
        if [ ! -d "node_modules" ]; then
            echo -e "${BLUE}🔹 Installing Seeder Dependencies...${NC}"
            npm install
        fi
        
        echo -e "${YELLOW}🔹 Resetting Database...${NC}"
        npm run seed:reset
        cd ..
        
        # Start backend
        echo -e "${BLUE}🔹 Starting Backend...${NC}"
        (cd backend && go run cmd/api/main.go) &
        BACKEND_PID=$!
        
        echo -e "${YELLOW}⏳ Waiting for Backend...${NC}"
        sleep 5
        
        # Seed
        echo -e "${BLUE}🔹 Seeding Database (30 companies, 990 jobs)...${NC}"
        cd seeder-api && npm run seed && cd ..
        
        # Frontend
        if [ ! -d "frontend/node_modules" ]; then
            cd frontend && npm install && cd ..
        fi
        
        echo -e "${BLUE}🔹 Starting Frontend...${NC}"
        (cd frontend && npm run dev -- -p 8963) &
        FRONTEND_PID=$!
        
        echo -e "\n${GREEN}✅ Services running with fresh data!${NC}"
        echo -e "   ${CYAN}Backend:${NC}  http://localhost:8521"
        echo -e "   ${CYAN}Frontend:${NC} http://localhost:8963"
        echo ""
        
        wait $BACKEND_PID $FRONTEND_PID
        ;;
        
    3)
        echo -e "\n${GREEN}📦 Starting All Services (Including Backoffice)...${NC}\n"
        
        # Backend
        cd backend && go mod tidy && cd ..
        
        # Dependencies
        [ ! -d "frontend/node_modules" ] && (cd frontend && npm install && cd ..)
        [ ! -d "backoffice/node_modules" ] && (cd backoffice && npm install && cd ..)
        
        echo -e "${BLUE}🔹 Starting Backend on port 8521...${NC}"
        (cd backend && go run cmd/api/main.go) &
        sleep 2
        
        echo -e "${BLUE}🔹 Starting Frontend on port 8963...${NC}"
        (cd frontend && npm run dev -- -p 8963) &
        
        echo -e "${BLUE}🔹 Starting Backoffice on port 3001...${NC}"
        (cd backoffice && npm run start:dev) &
        
        echo -e "\n${GREEN}✅ All services running:${NC}"
        echo -e "   ${CYAN}Backend:${NC}    http://localhost:8521"
        echo -e "   ${CYAN}Frontend:${NC}   http://localhost:8963"
        echo -e "   ${CYAN}Backoffice:${NC} http://localhost:3001"
        echo -e "   ${CYAN}Swagger:${NC}    http://localhost:3001/api/docs"
        echo ""
        
        wait
        ;;
        
    4)
        echo -e "\n${GREEN}🧪 Running Backend E2E Tests...${NC}\n"
        cd backend && go test -tags=e2e -v ./tests/e2e/... 2>&1
        echo -e "\n${GREEN}✅ Tests completed!${NC}"
        ;;
        
    5)
        echo -e "\n${GREEN}🌿 Seeding Database Only...${NC}\n"
        cd seeder-api
        [ ! -d "node_modules" ] && npm install
        npm run seed
        echo -e "\n${GREEN}✅ Seeding completed!${NC}"
        ;;
        
    0)
        echo -e "${YELLOW}Bye! 👋${NC}"
        exit 0
        ;;
        
    *)
        echo -e "${RED}Invalid option. Exiting.${NC}"
        exit 1
        ;;
esac
