name: 'Version Generator'
description: 'Generates version files based on git information'
author: 'Wellsite Navigator'
branding:
  icon: 'tag'
  color: 'orange'

inputs:
  dir:
    description: 'Directory to use for command execution and output file path'
    required: false
    default: '.'
  outputFilePath:
    description: 'Output file path (relative to dir if not absolute) where the version file should be written'
    required: false
  format:
    description: 'Output format (string or json)'
    required: false
    default: 'string'
  android:
    description: 'Enable Android version code generation'
    required: false
    default: 'false'
  android-package:
    description: 'Android package name'
    required: false
  android-service-account-key:
    description: 'Service account key JSON for Google Play API authentication'
    required: false
  android-track:
    description: 'Track to check for version codes'
    required: false
  android-major-increment:
    description: 'Increment to add to version code on major version change (default: 10)'
    required: false
    default: '10'
  ios:
    description: 'Enable iOS build number generation'
    required: false
    default: 'false'
  ios-bundle-id:
    description: 'iOS bundle ID'
    required: false
  ios-api-key-id:
    description: 'App Store Connect API Key ID'
    required: false
  ios-api-issuer-id:
    description: 'App Store Connect API Issuer ID'
    required: false
  ios-api-private-key:
    description: 'App Store Connect API Private Key (can be base64-encoded)'
    required: false

outputs:
  version:
    description: 'The generated version string'
    value: ${{ steps.generate-version.outputs.version }}
  major:
    description: 'The major version number'
    value: ${{ steps.generate-version.outputs.major }}
  minor:
    description: 'The minor version number'
    value: ${{ steps.generate-version.outputs.minor }}
  patch:
    description: 'The patch version number (commit count since the last tag)'
    value: ${{ steps.generate-version.outputs.patch }}
  branchName:
    description: 'The current branch name'
    value: ${{ steps.generate-version.outputs.branchName }}
  commitHash:
    description: 'The current commit hash'
    value: ${{ steps.generate-version.outputs.commitHash }}
  appReleaseVersion:
    description: 'The app release version (only major.minor.patch) for mobile app versioning'
    value: ${{ steps.generate-version.outputs.appReleaseVersion }}
  androidVersionCode:
    description: 'The Android version code (if Android version code generation is enabled)'
    value: ${{ steps.generate-version.outputs.androidVersionCode }}
  iosBuildNumber:
    description: 'The iOS build number (if iOS build number generation is enabled)'
    value: ${{ steps.generate-version.outputs.iosBuildNumber }}
  iosBuildNumberString:
    description: 'The iOS build number in string format (if iOS build number generation is enabled)'
    value: ${{ steps.generate-version.outputs.iosBuildNumberString }}

runs:
  using: 'composite'
  steps:
    - name: Setup Node.js
      uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
      with:
        node-version: '22'

    - name: Install dependencies
      shell: bash
      run: npm install -g --ignore-scripts @wellsite/version-generator

    - name: Generate version
      id: generate-version
      shell: bash
      run: |
        # Prepare Android options if enabled
        ANDROID_OPTIONS=""
        if [ "${{ inputs.android }}" = "true" ]; then
          ANDROID_OPTIONS="--android"
          
          if [ ! -z "${{ inputs.android-package }}" ]; then
            ANDROID_OPTIONS="$ANDROID_OPTIONS --android-package ${{ inputs.android-package }}"
          fi
          
          if [ ! -z "${{ inputs.android-service-account-key }}" ]; then
            ANDROID_OPTIONS="$ANDROID_OPTIONS --android-service-account-key \"${{ inputs.android-service-account-key }}\""
          fi
          
          if [ ! -z "${{ inputs.android-track }}" ]; then
            ANDROID_OPTIONS="$ANDROID_OPTIONS --android-track ${{ inputs.android-track }}"
          fi
          
          if [ ! -z "${{ inputs.android-major-increment }}" ]; then
            ANDROID_OPTIONS="$ANDROID_OPTIONS --android-major-increment ${{ inputs.android-major-increment }}"
          fi
        fi

        # Prepare iOS options if enabled
        IOS_OPTIONS=""
        if [ "${{ inputs.ios }}" = "true" ]; then
          IOS_OPTIONS="--ios"
          
          if [ ! -z "${{ inputs.ios-bundle-id }}" ]; then
            IOS_OPTIONS="$IOS_OPTIONS --ios-bundle-id ${{ inputs.ios-bundle-id }}"
          fi
          
          if [ ! -z "${{ inputs.ios-api-key-id }}" ]; then
            IOS_OPTIONS="$IOS_OPTIONS --ios-api-key-id ${{ inputs.ios-api-key-id }}"
          fi
          
          if [ ! -z "${{ inputs.ios-api-issuer-id }}" ]; then
            IOS_OPTIONS="$IOS_OPTIONS --ios-api-issuer-id ${{ inputs.ios-api-issuer-id }}"
          fi
          
          if [ ! -z "${{ inputs.ios-api-private-key }}" ]; then
            IOS_OPTIONS="$IOS_OPTIONS --ios-api-private-key \"${{ inputs.ios-api-private-key }}\""
          fi
        fi

        # Always generate JSON format for parsing components
        VERSION_JSON=$(generate-version --dir "${{ inputs.dir }}" --format json $ANDROID_OPTIONS $IOS_OPTIONS)

        # If outputFilePath is specified, write the output in the desired format
        if [ ! -z "${{ inputs.outputFilePath }}" ]; then
          # If outputFilePath is an absolute path, use it directly, otherwise join with dir
          if [[ "${{ inputs.outputFilePath }}" == /* ]]; then
            DEST_PATH="${{ inputs.outputFilePath }}"
          else
            DEST_PATH="${{ inputs.dir }}/${{ inputs.outputFilePath }}"
          fi
          mkdir -p "$(dirname "$DEST_PATH")"

          if [ "${{ inputs.format }}" = "json" ]; then
            # Write JSON format
            echo "$VERSION_JSON" > "$DEST_PATH"
          else
            # Write string format
            VERSION=$(echo "$VERSION_JSON" | jq -r '.version')
            echo "$VERSION" > "$DEST_PATH"
          fi

          echo "Wrote version to $DEST_PATH in ${{ inputs.format }} format"
        fi

        # Extract components from JSON
        VERSION=$(echo "$VERSION_JSON" | jq -r '.version')
        MAJOR=$(echo "$VERSION_JSON" | jq -r '.major')
        MINOR=$(echo "$VERSION_JSON" | jq -r '.minor')
        PATCH=$(echo "$VERSION_JSON" | jq -r '.patch')
        BRANCH_NAME=$(echo "$VERSION_JSON" | jq -r '.branchName')
        COMMIT_HASH=$(echo "$VERSION_JSON" | jq -r '.commitHash')
        APP_RELEASE_VERSION=$(echo "$VERSION_JSON" | jq -r '.appReleaseVersion')

        # Set outputs
        echo "version=${VERSION}" >> $GITHUB_OUTPUT
        echo "major=${MAJOR}" >> $GITHUB_OUTPUT
        echo "minor=${MINOR}" >> $GITHUB_OUTPUT
        echo "patch=${PATCH}" >> $GITHUB_OUTPUT
        echo "branchName=${BRANCH_NAME}" >> $GITHUB_OUTPUT
        echo "commitHash=${COMMIT_HASH}" >> $GITHUB_OUTPUT
        echo "appReleaseVersion=${APP_RELEASE_VERSION}" >> $GITHUB_OUTPUT

        # Extract and set Android version code if available
        ANDROID_VERSION_CODE=$(echo "$VERSION_JSON" | jq -r '.androidVersionCode // ""')
        if [ ! -z "$ANDROID_VERSION_CODE" ]; then
          echo "androidVersionCode=${ANDROID_VERSION_CODE}" >> $GITHUB_OUTPUT
        fi

        # Extract and set iOS build number if available
        IOS_BUILD_NUMBER=$(echo "$VERSION_JSON" | jq -r '.iosBuildNumber // ""')
        if [ ! -z "$IOS_BUILD_NUMBER" ]; then
          echo "iosBuildNumber=${IOS_BUILD_NUMBER}" >> $GITHUB_OUTPUT
        fi

        # Extract and set iOS build number string if available
        IOS_BUILD_NUMBER_STRING=$(echo "$VERSION_JSON" | jq -r '.iosBuildNumberString // ""')
        if [ ! -z "$IOS_BUILD_NUMBER_STRING" ]; then
          echo "iosBuildNumberString=${IOS_BUILD_NUMBER_STRING}" >> $GITHUB_OUTPUT
        fi

        # Display output
        echo "Generated version: ${VERSION}"
        echo "Major: ${MAJOR}"
        echo "Minor: ${MINOR}"
        echo "Patch: ${PATCH}"
        echo "Branch: ${BRANCH_NAME}"
        echo "Commit: ${COMMIT_HASH}"
        echo "App Release Version: ${APP_RELEASE_VERSION}"

        if [ ! -z "$ANDROID_VERSION_CODE" ]; then
          echo "Android Version Code: ${ANDROID_VERSION_CODE}"
        fi

        if [ ! -z "$IOS_BUILD_NUMBER" ]; then
          echo "iOS Build Number: ${IOS_BUILD_NUMBER}"
        fi

        if [ ! -z "$IOS_BUILD_NUMBER_STRING" ]; then
          echo "iOS Build Number String: ${IOS_BUILD_NUMBER_STRING}"
        fi
