#!/bin/bash
# EN: Check development environment for GoodGo microservices
# VI: Kiểm tra môi trường phát triển cho GoodGo microservices

set -e

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  Development Environment Check"
echo "  Kiểm tra Môi Trường Phát Triển"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

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

check_command() {
    local cmd=$1
    local required_version=$2
    
    if command -v $cmd &> /dev/null; then
        local version=$($cmd --version 2>&1 | head -n 1)
        echo -e "${GREEN}✓${NC} $cmd: $version"
        return 0
    else
        echo -e "${RED}✗${NC} $cmd: Not installed"
        if [ ! -z "$required_version" ]; then
            echo -e "  ${YELLOW}Required: $required_version${NC}"
        fi
        return 1
    fi
}

check_dotnet() {
    if command -v dotnet &> /dev/null; then
        local version=$(dotnet --version)
        echo -e "${GREEN}✓${NC} .NET SDK: $version"
        
        # Check if version >= 8.0
        local major=$(echo $version | cut -d. -f1)
        if [ "$major" -lt 8 ]; then
            echo -e "  ${YELLOW}Warning: .NET 8.0+ recommended${NC}"
        fi
        return 0
    else
        echo -e "${RED}✗${NC} .NET SDK: Not installed"
        echo -e "  ${YELLOW}Required: .NET 8.0+${NC}"
        return 1
    fi
}

check_docker() {
    if command -v docker &> /dev/null; then
        local version=$(docker --version | cut -d' ' -f3 | tr -d ',')
        echo -e "${GREEN}✓${NC} Docker: $version"
        
        # Check if Docker daemon is running
        if docker ps &> /dev/null; then
            echo -e "  ${GREEN}Docker daemon is running${NC}"
        else
            echo -e "  ${RED}Docker daemon is not running${NC}"
            return 1
        fi
        return 0
    else
        echo -e "${RED}✗${NC} Docker: Not installed"
        return 1
    fi
}

check_docker_compose() {
    if command -v docker-compose &> /dev/null; then
        local version=$(docker-compose --version | cut -d' ' -f4 | tr -d ',')
        echo -e "${GREEN}✓${NC} docker-compose: $version"
        return 0
    elif docker compose version &> /dev/null; then
        local version=$(docker compose version | cut -d' ' -f4 | tr -d 'v')
        echo -e "${GREEN}✓${NC} docker compose: $version"
        return 0
    else
        echo -e "${RED}✗${NC} docker-compose: Not installed"
        return 1
    fi
}

# Main checks
echo "Core Tools:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
ERRORS=0

check_dotnet || ((ERRORS++))
check_docker || ((ERRORS++))
check_docker_compose || ((ERRORS++))

echo ""
echo "Optional Tools:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

check_command "git" || true
check_command "curl" || true
check_command "jq" || true

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

if [ $ERRORS -eq 0 ]; then
    echo -e "${GREEN}✓ All core tools are installed!${NC}"
    echo ""
    exit 0
else
    echo -e "${RED}✗ $ERRORS core tool(s) missing or not working${NC}"
    echo ""
    echo "Installation guides:"
    echo "  .NET SDK: https://dotnet.microsoft.com/download"
    echo "  Docker: https://docs.docker.com/get-docker/"
    echo ""
    exit 1
fi
