#!/bin/bash

set -e

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

echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}   Admin Vertex Vibes - Quick Installer${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

# Check for required tools
echo -e "${YELLOW}[1/8]${NC} Checking requirements..."
for cmd in git npm curl unzip; do
    if ! command -v $cmd &> /dev/null; then
        echo -e "${RED}Error: $cmd is not installed. Please install it first.${NC}"
        exit 1
    fi
done
echo -e "${GREEN}✓ All requirements met${NC}"

# Clone the repository to Desktop
INSTALL_DIR="$HOME/Desktop/admin-vertex-vibes"
echo ""
echo -e "${YELLOW}[2/8]${NC} Cloning repository to Desktop..."
if [ -d "$INSTALL_DIR" ]; then
    echo -e "${YELLOW}  Directory already exists. Removing...${NC}"
    rm -rf "$INSTALL_DIR"
fi
git clone https://github.com/MathGaps/admin-vibes.git "$INSTALL_DIR"
echo -e "${GREEN}✓ Repository cloned to ~/Desktop/admin-vertex-vibes${NC}"

# Navigate to video_generator
echo ""
echo -e "${YELLOW}[3/8]${NC} Navigating to video_generator..."
cd "$INSTALL_DIR/video_generator"
echo -e "${GREEN}✓ In video_generator directory${NC}"

# Download secrets zip
echo ""
echo -e "${YELLOW}[4/8]${NC} Downloading secrets..."
curl -fsSL -o secrets.zip "https://tuterocdn.hemanta.dev/vx-res-7b2e.zip"
echo -e "${GREEN}✓ Secrets downloaded${NC}"

# Unzip secrets
echo ""
echo -e "${YELLOW}[5/8]${NC} Extracting secrets..."
unzip -o secrets.zip
rm secrets.zip
echo -e "${GREEN}✓ Secrets extracted${NC}"

# Download .env.local
echo ""
echo -e "${YELLOW}[6/8]${NC} Downloading environment config..."
curl -fsSL -o .env.local "https://tuterocdn.hemanta.dev/vx-cfg-8a3f.dat"
echo -e "${GREEN}✓ Environment config downloaded${NC}"

# Install dependencies
echo ""
echo -e "${YELLOW}[7/8]${NC} Installing dependencies (this may take a few minutes)..."
npm install
echo -e "${GREEN}✓ Dependencies installed${NC}"

# Kill process on port 3000 if exists
echo ""
echo -e "${YELLOW}[8/8]${NC} Checking port 3000..."
for pid in $(lsof -ti:3000 2>/dev/null); do
    echo -e "${YELLOW}  Killing process on port 3000 (PID: $pid)${NC}"
    kill -9 "$pid" 2>/dev/null || true
done
sleep 1
# Double check
if lsof -ti:3000 >/dev/null 2>&1; then
    echo -e "${RED}Warning: Could not kill process on port 3000. You may need to do it manually.${NC}"
else
    echo -e "${GREEN}✓ Port 3000 is free${NC}"
fi

echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${GREEN}✓ Installation complete!${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
echo -e "${YELLOW}Starting the app and opening browser...${NC}"
echo -e "${YELLOW}Press Ctrl+C to stop the server when done.${NC}"
echo ""

# Open browser after a short delay (in background, detached from terminal)
open_browser() {
    sleep 8
    if command -v open &>/dev/null; then
        open "http://localhost:3000"
    elif command -v xdg-open &>/dev/null; then
        xdg-open "http://localhost:3000"
    elif command -v start &>/dev/null; then
        start "http://localhost:3000"
    fi
}
nohup bash -c "$(declare -f open_browser); open_browser" >/dev/null 2>&1 &
disown 2>/dev/null || true

# Start the dev server
npm run dev
