# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

name: 'Update Package Version'
description: 'Updates pyproject.toml version with a provided timestamp'
inputs:
  timestamp:
    description: 'Timestamp to override to the package version'
    required: true
runs:
  using: "composite"
  steps:
    - name: Setup Python
      uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
      with:
        python-version: '3.12'

    - name: Install toml
      run: pip install toml
      shell: bash

    - name: Get and update version
      shell: bash
      env:
        TIMESTAMP: ${{ inputs.timestamp }}
      run: |
        # Read the current version from the Rust crate's Cargo.toml
        CURRENT_VERSION=$(python -c "import toml; print(toml.load('bindings/python/Cargo.toml')['package']['version'])")
        export NEW_VERSION="${CURRENT_VERSION}.dev${TIMESTAMP}"
        echo "Current version: ${CURRENT_VERSION}"
        echo "New dev version: ${NEW_VERSION}"

        # Update bindings/python/pyproject.toml:
        # - Set project.version = NEW_VERSION
        # - Ensure 'version' is not listed in project.dynamic
        python -c "
        import toml
        import os
        config = toml.load('bindings/python/pyproject.toml')
        new_version = os.environ['NEW_VERSION']
        config['project']['version'] = new_version
        if 'dynamic' in config['project']:
          config['project']['dynamic'] = [v for v in config['project']['dynamic'] if v != 'version']
        with open('bindings/python/pyproject.toml', 'w') as f:
            toml.dump(config, f)
        print(f'Updated version to: {new_version}')
        "