#!/bin/bash
# Bash script for local setup on Linux/macOS
# Run this script from the payment directory

echo "=== Payment Service Local Setup ==="
echo ""

# Check if Go is installed
echo "Checking prerequisites..."
if command -v go &> /dev/null; then
    GO_VERSION=$(go version)
    echo "✓ Go installed: $GO_VERSION"
else
    echo "✗ Go is not installed. Please install Go 1.21+ from https://golang.org/dl/"
    exit 1
fi

# Check if Docker is installed (optional)
if command -v docker &> /dev/null; then
    DOCKER_VERSION=$(docker --version)
    echo "✓ Docker installed: $DOCKER_VERSION"
    read -p "Do you want to use Docker for PostgreSQL and Redis? (y/n): " use_docker
else
    echo "⚠ Docker not found. Will use manual setup."
    use_docker="n"
fi

# Setup with Docker
if [ "$use_docker" = "y" ] || [ "$use_docker" = "Y" ]; then
    echo ""
    echo "Setting up with Docker..."
    
    # Check if docker-compose.local.yml exists
    if [ -f "docker-compose.local.yml" ]; then
        echo "Starting PostgreSQL and Redis containers..."
        docker-compose -f docker-compose.local.yml up -d
        
        echo "Waiting for services to be ready..."
        sleep 5
        
        echo "✓ Docker services started"
    else
        echo "✗ docker-compose.local.yml not found. Please create it first."
        exit 1
    fi
else
    echo ""
    echo "Manual setup selected."
    echo "Please ensure PostgreSQL and Redis are installed and running:"
    echo "  - PostgreSQL on localhost:5432"
    echo "  - Redis on localhost:6379"
    echo ""
fi

# Check if .env exists
if [ ! -f ".env" ]; then
    echo "Creating .env file from example..."
    cp env.example .env
    echo "✓ .env file created"
    echo ""
    echo "⚠ IMPORTANT: Please edit .env file with your local settings:"
    echo "  - DB_PASSWORD (if using Docker: local_dev_password)"
    echo "  - JWT_SECRET (use a secure random string)"
    echo ""
    read -p "Do you want to edit .env now? (y/n): " edit_now
    if [ "$edit_now" = "y" ] || [ "$edit_now" = "Y" ]; then
        ${EDITOR:-nano} .env
    fi
else
    echo "✓ .env file already exists"
fi

# Install Go dependencies
echo ""
echo "Installing Go dependencies..."
go mod download
if [ $? -eq 0 ]; then
    echo "✓ Dependencies installed"
else
    echo "✗ Failed to install dependencies"
    exit 1
fi

# Test database connection (if psql is available)
if command -v psql &> /dev/null; then
    echo ""
    echo "Testing database connection..."
    export PGPASSWORD="local_dev_password"
    psql -h localhost -U payment_user -d internet -c "SELECT 1;" &> /dev/null
    if [ $? -eq 0 ]; then
        echo "✓ Database connection successful"
    else
        echo "⚠ Database connection failed. Please check your .env settings"
    fi
    unset PGPASSWORD
else
    echo "⚠ psql not found. Skipping database connection test"
fi

# Test Redis connection (if redis-cli is available)
if command -v redis-cli &> /dev/null; then
    REDIS_TEST=$(redis-cli ping 2>&1)
    if [ "$REDIS_TEST" = "PONG" ]; then
        echo "✓ Redis connection successful"
    else
        echo "⚠ Redis connection failed. Please ensure Redis is running"
    fi
else
    echo "⚠ redis-cli not found. Skipping Redis connection test"
fi

echo ""
echo "=== Setup Complete ==="
echo ""
echo "To start the payment service, run:"
echo "  go run main.go"
echo ""
echo "The service will be available at: http://localhost:8080"
echo ""
echo "Useful commands:"
echo "  - Health check: curl http://localhost:8080/health"
echo "  - View logs: Check console output"
echo "  - Stop Docker services: docker-compose -f docker-compose.local.yml down"
echo ""

