name: Build and Publish Docker Image

on:
    push:
        tags:
            - "v*"
    workflow_dispatch:
        inputs:
            version:
                description: "Version tag (e.g., v1.0.0)"
                required: true
                type: string

env:
    IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/lidify

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Free up disk space
              run: |
                  sudo rm -rf /usr/share/dotnet
                  sudo rm -rf /opt/ghc
                  sudo rm -rf /usr/local/share/boost
                  sudo rm -rf "$AGENT_TOOLSDIRECTORY"

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

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

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

            - name: Extract version
              id: version
              run: |
                  if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
                    echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
                  else
                    echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
                  fi

            - name: Build and push
              uses: docker/build-push-action@v5
              with:
                  context: .
                  file: ./Dockerfile
                  push: true
                  tags: |
                      ${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
                      ${{ env.IMAGE_NAME }}:latest
                  cache-from: type=gha
                  cache-to: type=gha,mode=max
                  # Note: ARM64 removed due to QEMU emulation issues with npm packages
                  # Can be re-added when using native ARM64 runners
                  platforms: linux/amd64

    create-release:
        needs: [build]
        runs-on: ubuntu-latest
        permissions:
            contents: write
        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Extract version
              id: version
              run: |
                  if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
                    echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
                  else
                    echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
                  fi

            - name: Create Release
              uses: softprops/action-gh-release@v1
              with:
                  tag_name: ${{ steps.version.outputs.version }}
                  name: Lidify ${{ steps.version.outputs.version }}
                  body: |
                      ## Quick Start

                      ```bash
                      docker run -d \
                        --name lidify \
                        -p 3030:3030 \
                        -v /path/to/your/music:/music \
                        -v lidify_data:/data \
                        ${{ secrets.DOCKERHUB_USERNAME }}/lidify:${{ steps.version.outputs.version }}
                      ```

                      Then open http://localhost:3030 and create your account!

                      ## Android App

                      Download the APK below and install on your device.

                      ## Documentation

                      See the [README](https://github.com/${{ github.repository }}#readme) for full documentation.
                  draft: false
                  prerelease: false
                  generate_release_notes: true
