name: Build and Release

on:
    push:
        tags:
            - "*"
    workflow_dispatch:
        inputs:
            version:
                description: "Release version (e.g., v1.0.0)"
                required: false
                type: string
            build_docker:
                description: "Build Docker"
                required: false
                default: "true"
                type: boolean

permissions:
    contents: write
    packages: write

jobs:
    build:
        name: Build and Release
        runs-on: ubuntu-latest
        steps:
            - name: Clone Repo
              uses: https://git.quad4.io/actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
              with:
                  fetch-depth: 0

            - name: Determine version
              id: version
              run: |
                  if [ "${{ github.event_name }}" = "workflow_dispatch" ] && [ -n "${{ github.event.inputs.version }}" ]; then
                    echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
                  elif [[ "${{ github.ref }}" == refs/tags/* ]]; then
                    VERSION="${GITHUB_REF#refs/tags/}"
                    if [ -z "${VERSION}" ]; then
                      VERSION="${{ github.ref_name }}"
                    fi
                    if [ "${VERSION}" = "master" ] || [ "${VERSION}" = "main" ]; then
                      echo "Error: Invalid tag name '${VERSION}'. Tag name cannot be a branch name." >&2
                      exit 1
                    fi
                    echo "version=${VERSION}" >> $GITHUB_OUTPUT
                  else
                    SHORT_SHA=$(git rev-parse --short HEAD)
                    echo "version=${SHORT_SHA}" >> $GITHUB_OUTPUT
                  fi

            - name: Install NodeJS
              uses: https://git.quad4.io/actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
              with:
                  node-version: 22

            - name: Install Python
              uses: https://git.quad4.io/actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
              with:
                  python-version: "3.13"

            - name: Install Poetry
              run: python -m pip install --upgrade pip poetry>=2.0.0

            - name: Install pnpm
              uses: https://git.quad4.io/actions/setup-pnpm@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4
              with:
                  version: 10.0.0

            - name: Install system dependencies
              run: |
                  sudo apt-get update
                  sudo apt-get install -y patchelf libopusfile0 ffmpeg espeak-ng

            - name: Setup Task
              uses: https://git.quad4.io/actions/setup-task@0ab1b2a65bc55236a3bc64cde78f80e20e8885c2 # v1
              with:
                  version: "3.46.3"

            - name: Sync versions
              run: python scripts/sync_version.py

            - name: Install dependencies
              run: task install

            - name: Build Frontend
              run: task build-frontend

            - name: Build Python wheel
              run: task wheel

            - name: Build Electron App (Universal)
              run: pnpm run dist-prebuilt

            - name: Prepare release assets
              run: |
                  mkdir -p release-assets
                  # Collect artifacts
                  find dist -type f \( -name "*-linux*.AppImage" -o -name "*-linux*.deb" \) -exec cp {} release-assets/ \;
                  find python-dist -type f -name "*.whl" -exec cp {} release-assets/ \;

                  # Generate checksums
                  cd release-assets
                  echo "## SHA256 Checksums" > release-body.md
                  echo "" >> release-body.md
                  for file in *; do
                    if [ -f "$file" ] && [ "$file" != "release-body.md" ] && [[ "$file" != *.sha256 ]]; then
                      sha256sum "$file" | tee "${file}.sha256"
                      echo "\`$(cat "${file}.sha256")\`" >> release-body.md
                    fi
                  done

            - name: Validate version
              run: |
                  VERSION="${{ steps.version.outputs.version }}"
                  if [ -z "${VERSION}" ]; then
                    echo "Error: Version is empty" >&2
                    exit 1
                  fi
                  if [ "${VERSION}" = "master" ] || [ "${VERSION}" = "main" ]; then
                    echo "Error: Invalid version '${VERSION}'. Version cannot be a branch name." >&2
                    exit 1
                  fi
                  echo "Using version: ${VERSION}"

            - name: Create Release
              if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
              uses: https://git.quad4.io/actions/gitea-release-action@4875285c0950474efb7ca2df55233c51333eeb74 # v1
              with:
                  api_url: ${{ secrets.GITEA_API_URL }}
                  gitea_token: ${{ secrets.GITEA_TOKEN }}
                  title: ${{ steps.version.outputs.version }}
                  tag: ${{ steps.version.outputs.version }}
                  files: "release-assets/*"
                  bodyFile: "release-assets/release-body.md"
                  draft: false
                  prerelease: false

    # build_docker:
    #     runs-on: ubuntu-latest
    #     if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.build_docker == 'true')
    #     permissions:
    #         packages: write
    #         contents: read
    #     steps:
    #         - name: Clone Repo
    #           uses: https://git.quad4.io/actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

    #         - name: Set lowercase repository owner
    #           run: echo "REPO_OWNER_LC=${GITHUB_REPOSITORY_OWNER,,}" >> $GITHUB_ENV

    #         - name: Set up QEMU
    #           uses: https://git.quad4.io/actions/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0

    #         - name: Set up Docker Buildx
    #           uses: https://git.quad4.io/actions/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0

    #         - name: Log in to the GitHub Container registry
    #           uses: https://git.quad4.io/actions/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
    #           with:
    #               registry: ghcr.io
    #               username: ${{ github.actor }}
    #               password: ${{ secrets.GITHUB_TOKEN }}

    #         - name: Build and push Docker images
    #           uses: https://git.quad4.io/actions/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
    #           with:
    #               context: .
    #               platforms: linux/amd64,linux/arm64
    #               push: true
    #               tags: >-
    #                   ghcr.io/${{ env.REPO_OWNER_LC }}/reticulum-meshchatx:latest,
    #                   ghcr.io/${{ env.REPO_OWNER_LC }}/reticulum-meshchatx:${{ github.ref_name }}
    #               labels: >-
    #                   org.opencontainers.image.title=Reticulum MeshChatX,
    #                   org.opencontainers.image.description=Docker image for Reticulum MeshChatX,
    #                   org.opencontainers.image.url=https://github.com/${{ github.repository }}/pkgs/container/reticulum-meshchatx/
