#!/bin/bash
set -e

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

echo -e "${YELLOW}🔍 Verifying production setup...${NC}"

# Check Docker is running
if ! docker info > /dev/null 2>&1; then
  echo -e "${RED}✗ Docker is not running. Please start Docker and try again.${NC}"
  exit 1
fi
echo -e "${GREEN}✓ Docker is running${NC}"

# Check docker-compose file exists
if [ ! -f "docker-compose.yml" ]; then
  echo -e "${RED}✗ docker-compose.yml not found in the current directory${NC}"
  exit 1
fi
echo -e "${GREEN}✓ docker-compose.yml found${NC}"

# Check required environment variables
REQUIRED_ENV_VARS=("VITE_API_URL" "SPOTIFY_CLIENT_ID" "SPOTIFY_CLIENT_SECRET")
MISSING_VARS=()

for var in "${REQUIRED_ENV_VARS[@]}"; do
  if [ -z "${!var}" ]; then
    MISSING_VARS+=("$var")
  fi
done

if [ ${#MISSING_VARS[@]} -ne 0 ]; then
  echo -e "${YELLOW}⚠ The following required environment variables are not set:${NC}"
  for var in "${MISSING_VARS[@]}"; do
    echo -e "  - ${RED}$var${NC}"
  done
  echo -e "${YELLOW}Please set these variables in your .env file or environment.${NC}"
  echo -e "${YELLOW}Continuing with verification, but the application may not work correctly.${NC}"
else
  echo -e "${GREEN}✓ All required environment variables are set${NC}"
fi

# Check if backend Dockerfile exists
if [ ! -f "server/Dockerfile" ]; then
  echo -e "${RED}✗ Backend Dockerfile not found at server/Dockerfile${NC}"
  exit 1
fi
echo -e "${GREEN}✓ Backend Dockerfile found${NC}"

# Check if UI Dockerfile exists
if [ ! -f "Dockerfile.ui" ]; then
  echo -e "${RED}✗ UI Dockerfile not found at Dockerfile.ui${NC}"
  exit 1
fi
echo -e "${GREEN}✓ UI Dockerfile found${NC}"

# Check if nginx config exists
if [ ! -f "docker/nginx.conf" ]; then
  echo -e "${RED}✗ Nginx configuration not found at docker/nginx.conf${NC}"
  exit 1
fi
echo -e "${GREEN}✓ Nginx configuration found${NC}"

# Check if error page exists
if [ ! -f "public/error.html" ]; then
  echo -e "${YELLOW}⚠ Custom error page not found at public/error.html${NC}"
else
  echo -e "${GREEN}✓ Custom error page found${NC}"
fi

# Check if health check endpoint exists
if [ ! -f "public/healthz" ]; then
  echo -e "${YELLOW}⚠ Health check endpoint not found at public/healthz${NC}"
else
  echo -e "${GREEN}✓ Health check endpoint found${NC}"
fi

# Check if docker-entrypoint.sh is executable
if [ -f "server/docker-entrypoint.sh" ] && [ ! -x "server/docker-entrypoint.sh" ]; then
  echo -e "${YELLOW}⚠ Making docker-entrypoint.sh executable...${NC}"
  chmod +x server/docker-entrypoint.sh
  echo -e "${GREEN}✓ docker-entrypoint.sh is now executable${NC}"
elif [ -f "server/docker-entrypoint.sh" ]; then
  echo -e "${GREEN}✓ docker-entrypoint.sh is executable${NC}"
else
  echo -e "${YELLOW}⚠ docker-entrypoint.sh not found in server/ directory${NC}"
fi

# Check for production build artifacts
if [ ! -d "dist" ]; then
  echo -e "${YELLOW}⚠ No production build found. Run 'yarn build' to create a production build.${NC}"
else
  echo -e "${GREEN}✓ Production build directory found${NC}"
fi

echo -e "\n${GREEN}✅ Production setup verification complete!${NC}"
echo -e "\nTo start the application in production mode, run:${YELLOW}"
echo -e "docker-compose up -d --build"
echo -e "${NC}\nTo view logs, run:${YELLOW}"
echo -e "docker-compose logs -f"
echo -e "${NC}\nTo stop the application, run:${YELLOW}"
echo -e "docker-compose down"
echo -e "${NC}"
