name: Create Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version for this release (e.g. 1.0.0)'
        required: true
        default: ''
      prerelease:
        description: 'Is this a pre-release?'
        required: true
        default: 'false'
        type: boolean
      description:
        description: 'Release description'
        required: false
        default: 'New release'

jobs:
  create-release:
    runs-on: macos
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          lfs: true
          fetch-depth: 0

      - name: Create Tag
        if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
        run: |
          TAG="v${{ github.run_number }}"
          echo "Creating git tag: $TAG"
          
          # Configure git with token authentication
          git config --global user.email "actions@gitea.com"
          git config --global user.name "Gitea Actions"
          
          # Direct token approach - simplest method
          git remote set-url origin "https://goran:${{ secrets.GITEATOKEN }}@luckyrobots.com/goran/lyra_game_ue.git"
          
          # Set git to not prompt for input
          export GIT_TERMINAL_PROMPT=0
          
          # Check if tag exists
          if ! git rev-parse "$TAG" >/dev/null 2>&1; then
            # Create tag without opening editor (-m flag)
            git tag -a "$TAG" -m "Release $TAG"
            
            # Push with timeout and debug
            echo "Pushing tag $TAG to origin..."
            git push --verbose origin "$TAG" || {
              echo "Error: Failed to push tag. Check your token permissions."
              exit 1
            }
            echo "Successfully created and pushed tag: $TAG"
          else
            echo "Tag $TAG already exists, skipping tag creation"
          fi
          echo "RELEASE_TAG=$TAG" >> $GITHUB_ENV

      - name: Create Gitea Release
        if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop'
        uses: https://gitea.com/actions/gitea-release-action@main
        with:
          token: ${{ secrets.GITEATOKEN }}
          tag_name: ${{ env.RELEASE_TAG }}
          title: "Release ${{ env.RELEASE_TAG }}"
          files: Builds/**