name: Version and Release

on:
  push:
    branches: [ main ]
    paths-ignore:
      - '**.md'
      - '.gitea/**'
      - '.drone.yml'

jobs:
  version-bump:
    runs-on: ubuntu-latest
    outputs:
      new_version: ${{ steps.version.outputs.new_version }}
      version_changed: ${{ steps.version.outputs.version_changed }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.GITEA_TOKEN || github.token }}
      
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
      
      - name: Configure Git
        run: |
          git config --global user.name 'Gitea Actions Bot'
          git config --global user.email 'actions@gitea.local'
      
      - name: Bump version
        id: version
        run: |
          # Get current version from backend package.json
          CURRENT_VERSION=$(node -p "require('./backend/package.json').version")
          echo "Current version: $CURRENT_VERSION"
          
          # Split version into parts
          IFS='.' read -r -a version_parts <<< "$CURRENT_VERSION"
          MAJOR="${version_parts[0]}"
          MINOR="${version_parts[1]}"
          PATCH="${version_parts[2]}"
          
          # Increment patch version
          NEW_PATCH=$((PATCH + 1))
          NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
          
          echo "New version: $NEW_VERSION"
          echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
          
          # Update version in package.json files
          cd backend && npm version $NEW_VERSION --no-git-tag-version
          cd ../frontend && npm version $NEW_VERSION --no-git-tag-version
          cd ..
          
          # Check if there are changes
          if [[ -n $(git status -s) ]]; then
            echo "version_changed=true" >> $GITHUB_OUTPUT
          else
            echo "version_changed=false" >> $GITHUB_OUTPUT
          fi
      
      - name: Commit version bump
        if: steps.version.outputs.version_changed == 'true'
        run: |
          git add backend/package.json backend/package-lock.json
          git add frontend/package.json frontend/package-lock.json
          git commit -m "chore: bump version to ${{ steps.version.outputs.new_version }}"
          git push
      
      - name: Create Git tag
        if: steps.version.outputs.version_changed == 'true'
        run: |
          git tag -a "v${{ steps.version.outputs.new_version }}" -m "Release v${{ steps.version.outputs.new_version }}"
          git push origin "v${{ steps.version.outputs.new_version }}"
  
  trigger-drone:
    needs: version-bump
    if: needs.version-bump.outputs.version_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Trigger Drone Build
        run: |
          echo "Version bumped to ${{ needs.version-bump.outputs.new_version }}"
          echo "Drone will automatically trigger on the new tag"
          # Drone CI will automatically trigger on the tag push event