name: Update release metadata

on:
    workflow_call:
        inputs:
            release_type:
                description: Release type
                type: string
                default: auto
            custom_version:
                description: The custom version to bump to (only for "custom" type)
                type: string
                default: ""
        outputs:
            version_number:
                description: The version number for the release
                value: ${{ jobs.release_metadata.outputs.version_number }}
            tag_name:
                description: The tag name for the release
                value: ${{ jobs.release_metadata.outputs.tag_name }}
            changelog:
                description: The full changelog content
                value: ${{ jobs.release_metadata.outputs.changelog }}
            release_notes:
                description: The release notes for this version
                value: ${{ jobs.release_metadata.outputs.release_notes }}
            changelog_commitish:
                description: The commit SHA after changelog update
                value: ${{ jobs.update_changelog.outputs.changelog_commitish }}

permissions:
    id-token: write
    contents: write

jobs:
    release_metadata:
        name: Prepare release metadata
        runs-on: ubuntu-latest
        outputs:
            version_number: ${{ steps.release_metadata.outputs.version_number }}
            tag_name: ${{ steps.release_metadata.outputs.tag_name }}
            changelog: ${{ steps.release_metadata.outputs.changelog }}
            release_notes: ${{ steps.release_metadata.outputs.release_notes }}
        steps:
            -   uses: apify/actions/git-cliff-release@v1.1.2
                name: Prepare release metadata
                id: release_metadata
                with:
                    release_type: ${{ inputs.release_type }}
                    custom_version: ${{ inputs.custom_version }}
                    existing_changelog_path: CHANGELOG.md
                    token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}

    update_changelog:
        needs: [ release_metadata ]
        name: Update changelog
        runs-on: ubuntu-latest
        outputs:
            changelog_commitish: ${{ steps.commit.outputs.commit_long_sha }}

        steps:
            -   name: Checkout repository
                uses: actions/checkout@v6
                with:
                    token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}

            # No Node/pnpm setup needed here — we only edit JSON files with jq,
            # which is preinstalled on GitHub-hosted runners.
            -   name: Update package version in package.json
                run: jq '.version = "${{ needs.release_metadata.outputs.version_number }}"' package.json > package.json.tmp && mv package.json.tmp package.json

            -   name: Update manifest.json version
                run: jq '.version = "${{ needs.release_metadata.outputs.version_number }}"' manifest.json > manifest.json.tmp && mv manifest.json.tmp manifest.json

            -   name: Update server.json version
                run: jq '.version = "${{ needs.release_metadata.outputs.version_number }}"' server.json > server.json.tmp && mv server.json.tmp server.json

            -   name: Update CHANGELOG.md
                uses: DamianReeves/write-file-action@master
                with:
                    path: CHANGELOG.md
                    write-mode: overwrite
                    contents: ${{ needs.release_metadata.outputs.changelog }}

            -   name: Commit changes
                id: commit
                uses: apify/actions/signed-commit@v1.0.0
                with:
                    message: "chore(release): Update changelog, package.json, manifest.json and server.json versions [skip ci]"
                    github-token: ${{ secrets.APIFY_SERVICE_ACCOUNT_GITHUB_TOKEN }}
