name: Create Release

on:
  push:
    tags:
      - 'v*'

env:
  REGISTRY: docker.io
  IMAGE_NAME_DISCORD_BOT: uptime-kuma-discord-bot
  IMAGE_NAME_WEB_BACKEND: uptime-kuma-web-backend

jobs:
  create-release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Extract version from tag
      id: version
      run: |
        VERSION=${GITHUB_REF#refs/tags/}
        VERSION=${VERSION#v}
        echo "version=$VERSION" >> $GITHUB_OUTPUT
        echo "tag=v$VERSION" >> $GITHUB_OUTPUT

    - name: Generate changelog
      id: changelog
      run: |
        # Get commits since last tag
        if git describe --tags --abbrev=0 HEAD~1 >/dev/null 2>&1; then
          PREV_TAG=$(git describe --tags --abbrev=0 HEAD~1)
          echo "Changes since $PREV_TAG:"
          git log --pretty=format:"- %s (%h)" $PREV_TAG..HEAD
        else
          echo "Initial release"
          git log --pretty=format:"- %s (%h)"
        fi > CHANGELOG.md

    - name: Create Release
      uses: softprops/action-gh-release@v1
      with:
        tag_name: ${{ steps.version.outputs.tag }}
        name: Release ${{ steps.version.outputs.tag }}
        body: |
          ## What's Changed
          
          ${{ steps.changelog.outputs.content }}
          
          ## Docker Images
          
          ### Discord Bot
          ```bash
          docker pull ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME_DISCORD_BOT }}:${{ steps.version.outputs.version }}
          docker pull ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME_DISCORD_BOT }}:latest
          ```
          
          ### Web Backend
          ```bash
          docker pull ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME_WEB_BACKEND }}:${{ steps.version.outputs.version }}
          docker pull ${{ env.REGISTRY }}/${{ secrets.DOCKER_USERNAME }}/${{ env.IMAGE_NAME_WEB_BACKEND }}:latest
          ```
          
          ## Quick Start
          
          ```bash
          # Clone the repository
          git clone <repository-url>
          cd UptimeKuma-DiscordBot
          
          # Copy environment template
          cp env.example .env
          
          # Edit .env with your values
          # Then start with Docker Compose
          docker-compose up -d
          ```
          
          See [DOCKER_SETUP.md](DOCKER_SETUP.md) for detailed instructions.
        files: |
          CHANGELOG.md
        draft: false
        prerelease: false

    - name: Update Docker Hub description
      if: success()
      run: |
        echo "🎉 Release ${{ steps.version.outputs.tag }} created successfully!"
        echo "Docker images will be automatically built and pushed by the docker-build workflow."
