#!/bin/bash

# Test script for Placebo.mk Docker setup

set -e

echo "🧪 Testing Placebo.mk Docker setup..."

# Check if docker-compose is available
if ! command -v docker-compose &> /dev/null; then
    echo "⚠️  docker-compose not found, using docker compose"
    DOCKER_COMPOSE="docker compose"
else
    DOCKER_COMPOSE="docker-compose"
fi

# Create environment files if they don't exist
echo "📝 Creating environment files..."

# Backend .env
if [ ! -f backend/.env ]; then
    cat > backend/.env << EOF
# Backend Environment Variables
NODE_ENV=production
DATABASE_TYPE=postgres
DATABASE_HOST=postgres
DATABASE_PORT=5432
DATABASE_USERNAME=placebo_user
DATABASE_PASSWORD=placebo_password
DATABASE_NAME=placebo_db
DATABASE_SYNCHRONIZE=false
DATABASE_LOGGING=false
JWT_SECRET=your-jwt-secret-key-change-in-production
JWT_EXPIRATION=3600
CORS_ORIGIN=http://localhost:5173,http://localhost:3001
PORT=3000
EOF
    echo "✅ Created backend/.env"
fi

# Frontend .env
if [ ! -f frontend/.env ]; then
    cat > frontend/.env << EOF
# Frontend Environment Variables
NODE_ENV=production
VITE_API_URL=http://localhost:3000
VITE_CMS_URL=http://localhost:1337
EOF
    echo "✅ Created frontend/.env"
fi

# CMS .env
if [ ! -f cms/cms/.env ]; then
    cat > cms/cms/.env << EOF
# CMS Environment Variables
NODE_ENV=production
DATABASE_CLIENT=postgres
DATABASE_HOST=postgres
DATABASE_PORT=5432
DATABASE_NAME=placebo_db
DATABASE_USERNAME=placebo_user
DATABASE_PASSWORD=placebo_password
DATABASE_SSL=false
JWT_SECRET=your-jwt-secret-key-change-in-production
ADMIN_JWT_SECRET=your-admin-jwt-secret-key-change-in-production
APP_KEYS=your-app-keys-change-in-production
API_TOKEN_SALT=your-api-token-salt-change-in-production
TRANSFER_TOKEN_SALT=your-transfer-token-salt-change-in-production
CORS_ORIGIN=http://localhost:5173,http://localhost:3001
PORT=1337
EOF
    echo "✅ Created cms/cms/.env"
fi

# Create database initialization script
mkdir -p scripts
cat > scripts/init-db.sql << EOF
-- Create separate databases for backend and CMS
CREATE DATABASE placebo_backend_db;
CREATE DATABASE placebo_cms_db;

-- Grant privileges
GRANT ALL PRIVILEGES ON DATABASE placebo_backend_db TO placebo_user;
GRANT ALL PRIVILEGES ON DATABASE placebo_cms_db TO placebo_user;
EOF
echo "✅ Created database initialization script"

echo "🐳 Building Docker images..."
$DOCKER_COMPOSE build

echo "🚀 Starting services..."
$DOCKER_COMPOSE up -d

echo "⏳ Waiting for services to be healthy..."
sleep 30

echo "📊 Checking service status..."
$DOCKER_COMPOSE ps

echo "🔍 Testing service connectivity..."

# Test PostgreSQL
echo "Testing PostgreSQL..."
if docker exec placebo-postgres pg_isready -U placebo_user -d placebo_db; then
    echo "✅ PostgreSQL is healthy"
else
    echo "❌ PostgreSQL health check failed"
    exit 1
fi

# Test Backend
echo "Testing Backend API..."
if curl -f http://localhost:3000/health > /dev/null 2>&1; then
    echo "✅ Backend API is healthy"
else
    echo "❌ Backend API health check failed"
    exit 1
fi

# Test CMS
echo "Testing CMS..."
if curl -f http://localhost:1337/_health > /dev/null 2>&1; then
    echo "✅ CMS is healthy"
else
    echo "❌ CMS health check failed"
    exit 1
fi

# Test Frontend
echo "Testing Frontend..."
if curl -f http://localhost:3001 > /dev/null 2>&1; then
    echo "✅ Frontend is serving"
else
    echo "❌ Frontend health check failed"
    exit 1
fi

echo ""
echo "🎉 All tests passed! Docker setup is working correctly."
echo ""
echo "Services are running at:"
echo "  Frontend: http://localhost:3001"
echo "  Backend API: http://localhost:3000"
echo "  CMS Admin: http://localhost:1337/admin"
echo ""
echo "To stop services: docker-compose down"
echo "To view logs: docker-compose logs -f"