name: Build Binaries

on:
  push:
    branches: [ "main" ]
    tags:
      - 'v*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build:
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

    steps:
    - uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.12'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pyinstaller
        pip install -r requirements.txt

    - name: Build Linux
      if: runner.os == 'Linux'
      run: |
        pyinstaller --name Amnezia-Web-Panel-Linux \
          --add-data "static:static" \
          --add-data "templates:templates" \
          --add-data "translations:translations" \
          --hidden-import uvicorn \
          --hidden-import fastapi \
          --clean \
          -y \
          -F app.py

    - name: Build Windows
      if: runner.os == 'Windows'
      run: |
        pyinstaller --name Amnezia-Web-Panel-Windows `
          --add-data "static;static" `
          --add-data "templates;templates" `
          --add-data "translations;translations" `
          --hidden-import uvicorn `
          --hidden-import fastapi `
          --clean `
          -y `
          -F app.py

    - name: Build macOS
      if: runner.os == 'macOS'
      run: |
        pyinstaller --name Amnezia-Web-Panel-macOS \
          --add-data "static:static" \
          --add-data "templates:templates" \
          --add-data "translations:translations" \
          --hidden-import uvicorn \
          --hidden-import fastapi \
          --clean \
          -y \
          -F app.py

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: binaries-${{ runner.os }}
        path: dist/*

  release:
    needs: build

    if: startsWith(github.ref, 'refs/tags/')

    runs-on: ubuntu-latest

    steps:
    - name: Download artifacts
      uses: actions/download-artifact@v4
      with:
        path: artifacts

    - name: Display structure
      run: ls -R artifacts

    - name: Publish Release
      uses: softprops/action-gh-release@v2
      with:
        generate_release_notes: true
        files: |
          artifacts/**/*

