#!/bin/bash
set -e

echo "🚀 Setting up RoadWave development environment..."

# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Check if Go is installed
if ! command -v go &> /dev/null; then
    echo "❌ Go is not installed. Please install Go 1.23 or later."
    exit 1
fi

echo "${BLUE}✓ Go version: $(go version)${NC}"

# Check if Docker is installed
if ! command -v docker &> /dev/null; then
    echo "❌ Docker is not installed. Please install Docker."
    exit 1
fi

echo "${BLUE}✓ Docker version: $(docker --version)${NC}"

# Install Go tools
echo "${GREEN}Installing Go tools...${NC}"
go install github.com/cosmtrek/air@latest
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
echo "${BLUE}✓ Go tools installed${NC}"

# Create .env file if it doesn't exist
if [ ! -f .env ]; then
    echo "${GREEN}Creating .env file from .env.example...${NC}"
    cp .env.example .env
    echo "${BLUE}✓ .env file created${NC}"
    echo "⚠️  Please update .env with your configuration"
else
    echo "${BLUE}✓ .env file already exists${NC}"
fi

# Download Go dependencies
echo "${GREEN}Downloading Go dependencies...${NC}"
go mod download
go mod tidy
echo "${BLUE}✓ Dependencies downloaded${NC}"

# Create necessary directories
mkdir -p tmp logs bin

echo ""
echo "${GREEN}✅ Setup complete!${NC}"
echo ""
echo "Next steps:"
echo "  1. Update .env with your configuration"
echo "  2. Start Docker services: make docker-up"
echo "  3. Run migrations: make migrate-up"
echo "  4. Start development server: make dev"
echo ""
