#!/bin/bash
# EN: Quick build script for a single service
# VI: Script build nhanh cho một service

set -e

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

if [ $# -eq 0 ]; then
    echo -e "${RED}Error: Service name required${NC}"
    echo "Usage: $0 <service-name>"
    echo "Example: $0 my-service-net"
    exit 1
fi

SERVICE_NAME=$1
SERVICE_DIR="services/${SERVICE_NAME}"

echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}  Quick Build: ${SERVICE_NAME}${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

# Check if service exists
if [ ! -d "$SERVICE_DIR" ]; then
    echo -e "${RED}✗ Service not found: $SERVICE_DIR${NC}"
    exit 1
fi

cd "$SERVICE_DIR"

# Find solution file
SOLUTION_FILE=$(find . -maxdepth 1 -name "*.slnx" -o -name "*.sln" | head -n 1)

if [ -z "$SOLUTION_FILE" ]; then
    echo -e "${RED}✗ No solution file found${NC}"
    exit 1
fi

echo -e "${YELLOW}📦 Restoring packages...${NC}"
dotnet restore "$SOLUTION_FILE"

echo ""
echo -e "${YELLOW}🔨 Building solution...${NC}"
dotnet build "$SOLUTION_FILE" --no-restore

echo ""
echo -e "${GREEN}✓ Build completed successfully!${NC}"
echo ""
echo "Next steps:"
echo "  • Run tests: dotnet test"
echo "  • Run locally: dotnet run --project src/*/API/"
echo "  • Build Docker: docker-compose -f deployments/local/docker-compose.yml build ${SERVICE_NAME}"
