#!/usr/bin/env bash
# sync-version.sh — Synchronize version across the project.
#
# Source of truth: apps/chanora_flutter/pubspec.yaml
#   Format: X.Y.Z-pre.N+BUILD   (e.g., 0.2.0-beta.1+75)
#
# Derived files:
#   Cargo.toml                                    (workspace version)
#   ios/Runner.xcodeproj/project.pbxproj         (MARKETING_VERSION)
#   macos/Runner.xcodeproj/project.pbxproj         (MARKETING_VERSION) — if exists
#   windows/Runner/Runner.rc                       (VERSION_AS_STRING)
#   tools/build-ios.sh                            (VERSION)
#   tools/build-windows.ps1                        ($Version)
#
# Usage:
#   ./tools/sync-version.sh                  # read from pubspec, write to all
#   ./tools/sync-version.sh 0.3.0-rc.1+80    # set version, then sync

set -euo pipefail
cd "$(dirname "$0")/.."

# --- Parse or set version ---
if [[ $# -gt 0 ]]; then
    NEW_VERSION="$1"
    # Update pubspec.yaml
    sed -i '' "s/^version: .*/version: ${NEW_VERSION}/" apps/chanora_flutter/pubspec.yaml
    echo "Set pubspec.yaml version to ${NEW_VERSION}"
else
    NEW_VERSION=$(grep '^version:' apps/chanora_flutter/pubspec.yaml | sed 's/version: //')
    echo "Using pubspec.yaml version: ${NEW_VERSION}"
fi

# --- Split version into components ---
# pubspec format: X.Y.Z-tag+BUILD  (Flutter uses + for build number)
FULL="${NEW_VERSION}"
BASE=$(echo "${FULL}" | sed 's/+.*//')       # 0.2.0-beta.1
MAJOR_MINOR_PATCH=$(echo "${BASE}" | sed 's/-.*//')  # 0.2.0
MARKETING=$(echo "${MAJOR_MINOR_PATCH}" | cut -d. -f1,2)  # 0.2
BUILD=$(echo "${FULL}" | sed 's/.*+//')         # 75
VVERSION="v${BASE}"                              # v0.2.0-beta.1

echo "  Full:           ${FULL}"
echo "  Base:           ${BASE}"
echo "  Major.Minor.Patch: ${MAJOR_MINOR_PATCH}"
echo "  Marketing:      ${MARKETING}"
echo "  Build:          ${BUILD}"
echo "  V-prefixed:     ${VVERSION}"

# --- Cargo.toml (workspace version = base without build number) ---
sed -i '' "s/^version = \".*\"/version = \"${BASE}\"/" Cargo.toml
echo "Updated Cargo.toml → ${BASE}"

# --- iOS Xcode project (MARKETING_VERSION) ---
sed -i '' "s/MARKETING_VERSION = .*;/MARKETING_VERSION = ${MARKETING};/g" \
    apps/chanora_flutter/ios/Runner.xcodeproj/project.pbxproj
echo "Updated ios/Runner.xcodeproj → MARKETING_VERSION = ${MARKETING}"

# --- macOS Xcode project (if exists) ---
if [[ -f apps/chanora_flutter/macos/Runner.xcodeproj/project.pbxproj ]]; then
    sed -i '' "s/MARKETING_VERSION = .*;/MARKETING_VERSION = ${MARKETING};/g" \
        apps/chanora_flutter/macos/Runner.xcodeproj/project.pbxproj
    echo "Updated macos/Runner.xcodeproj → MARKETING_VERSION = ${MARKETING}"
fi

# --- Windows Runner.rc ---
sed -i '' "s/VERSION_AS_STRING \".*\"/VERSION_AS_STRING \"${MAJOR_MINOR_PATCH}\"/g" \
    apps/chanora_flutter/windows/Runner/Runner.rc
echo "Updated windows/Runner/Runner.rc → ${MAJOR_MINOR_PATCH}"

# --- tools/build-ios.sh ---
sed -i '' "s/^VERSION=\".*\"/VERSION=\"${VVERSION}\"/" tools/build-ios.sh
echo "Updated tools/build-ios.sh → ${VVERSION}"

# --- tools/build-windows.ps1 ---
sed -i '' "s/\$Version = '.*'/\$Version = '${VVERSION}'/" tools/build-windows.ps1
echo "Updated tools/build-windows.ps1 → ${VVERSION}"

echo ""
echo "All versions synced to ${FULL}"