#!/bin/bash

set -e

echo "Setting up ChainID 138 Explorer Platform..."

# Check prerequisites
command -v docker >/dev/null 2>&1 || { echo "Docker is required but not installed. Aborting." >&2; exit 1; }
command -v go >/dev/null 2>&1 || { echo "Go is required but not installed. Aborting." >&2; exit 1; }
command -v node >/dev/null 2>&1 || { echo "Node.js is required but not installed. Aborting." >&2; exit 1; }

# Create .env file if it doesn't exist
if [ ! -f .env ]; then
    echo "Creating .env file from .env.example..."
    cp .env.example .env
    echo "Please edit .env file with your configuration"
fi

# Install backend dependencies
echo "Installing backend dependencies..."
cd backend
go mod download
cd ..

# Install frontend dependencies
echo "Installing frontend dependencies..."
cd frontend
npm install
cd ..

# Start infrastructure services
echo "Starting infrastructure services..."
docker-compose -f deployment/docker-compose.yml up -d postgres elasticsearch redis

echo "Waiting for services to be ready..."
sleep 10

# Run migrations
echo "Running database migrations..."
cd backend
go run database/migrations/migrate.go
cd ..

echo "Setup complete!"
echo "Next steps:"
echo "1. Edit .env file with your configuration"
echo "2. Run 'make dev' to start development environment"

