#!/bin/bash
# Build DanceLessonsCoach with version information
# Usage: ./scripts/build-with-version.sh [output_path]

set -e

# Default output path
OUTPUT_PATH="${1:-bin/server}"

# Get version from VERSION file or use default
if [ -f "VERSION" ]; then
    source VERSION
    VERSION="$MAJOR.$MINOR.$PATCH${PRERELEASE:+-$PRERELEASE}"
else
    VERSION="1.0.0"
fi

# Get git information
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
GIT_DATE=$(git log -1 --format=%cd --date=short 2>/dev/null || echo "unknown")

# Build time (UTC for consistency)
BUILD_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)

echo "🔧 Building DanceLessonsCoach $VERSION"
echo "   Commit: $GIT_COMMIT"
echo "   Date: $GIT_DATE"
echo "   Output: $OUTPUT_PATH"

# Build with ldflags to inject version information
go build \
    -o "$OUTPUT_PATH" \
    -ldflags="\
        -X DanceLessonsCoach/pkg/version.Version=$VERSION \
        -X DanceLessonsCoach/pkg/version.Commit=$GIT_COMMIT \
        -X DanceLessonsCoach/pkg/version.Date=$BUILD_DATE \
    " \
    ./cmd/server

echo "✅ Build complete: $OUTPUT_PATH"

# Show version info
"$OUTPUT_PATH" --version 2>/dev/null || echo "Version: $VERSION"