name: Docker Release

on:
  push:
    branches:
      - 'release/**'
  workflow_dispatch:

permissions:
  contents: write
  packages: write

env:
  CURRENT_BRANCH_NAME: ${{ github.head_ref || github.ref_name }}

jobs:
  extract-version:
    name: Extract Version
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.extract.outputs.version }}
      is_prerelease: ${{ steps.extract.outputs.is_prerelease }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v5
        with:
          token: ${{ secrets.PUBLISHER_TOKEN }}

      - name: Extract version from branch and version.py
        id: extract
        run: |
          # Extract version from branch name and validate against version.py
          .github/scripts/extract_version.sh "${{ env.CURRENT_BRANCH_NAME }}"

      - name: Validate version format
        run: |
          # Validate version is proper SemVer and set prerelease flag
          .github/scripts/validate_semver.sh "${{ steps.extract.outputs.version }}"

  docker-build:
    name: Build & Push Docker Images
    runs-on: ubuntu-latest
    needs: extract-version
    environment: prod

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Generate Docker tags
        id: docker-tags
        run: |
          VERSION="${{ needs.extract-version.outputs.version }}"
          IMAGE="rangermix/twitch-drops-miner"
          IS_PRERELEASE="${{ needs.extract-version.outputs.is_prerelease }}"

          # Always include the full version tag
          TAGS="$IMAGE:$VERSION"

          # For stable releases, add major.minor, major, and latest tags
          if [ "$IS_PRERELEASE" = "false" ]; then
            # Extract major.minor.patch
            MAJOR=$(echo "$VERSION" | cut -d. -f1)
            MINOR=$(echo "$VERSION" | cut -d. -f2)

            TAGS="$TAGS,$IMAGE:$MAJOR.$MINOR"
            TAGS="$TAGS,$IMAGE:$MAJOR"
            TAGS="$TAGS,$IMAGE:latest"
          fi

          echo "tags=$TAGS" >> "$GITHUB_OUTPUT"
          echo "Generated tags: $TAGS"

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.docker-tags.outputs.tags }}
          labels: |
            org.opencontainers.image.title=Twitch Drops Miner
            org.opencontainers.image.description=Automatically mine Twitch drops
            org.opencontainers.image.version=${{ needs.extract-version.outputs.version }}
            org.opencontainers.image.revision=${{ github.sha }}
            org.opencontainers.image.created=${{ github.event.repository.updated_at }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
          build-args: |
            BUILD_DATE=${{ github.event.repository.updated_at }}
            VCS_REF=${{ github.sha }}
            VERSION=${{ needs.extract-version.outputs.version }}

  trigger-github-release:
    name: Trigger GitHub Release Workflow
    runs-on: ubuntu-latest
    needs: docker-build

    steps:
      - name: Trigger Version Release Workflow
        uses: benc-uk/workflow-dispatch@v1
        with:
          workflow: github-release.yml
          token: ${{ secrets.PUBLISHER_TOKEN }}
          ref: ${{ github.ref }}
