name: Daily Build

on:
  schedule:
    # Run at 00:00 UTC every day
    - cron: '0 0 * * *'
  workflow_dispatch: # Allow manual trigger

permissions:
  contents: write

jobs:
  build:
    name: Build for ${{ matrix.os }}-${{ matrix.arch }}
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        include:
          # Linux builds
          - os: linux
            arch: amd64
            runner: ubuntu-latest
          - os: linux
            arch: arm64
            runner: ubuntu-24.04-arm

          # macOS builds
          - os: darwin
            arch: amd64
            runner: macos-latest
          - os: darwin
            arch: arm64
            runner: macos-latest

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

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.25.1'
          cache-dependency-path: agfs-server/go.sum

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

      - name: Install uv
        shell: bash
        run: |
          if [ "${{ matrix.os }}" = "windows" ]; then
            # For Windows, use PowerShell installer
            powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
            echo "$HOME/.cargo/bin" >> $GITHUB_PATH
            echo "$HOME/AppData/Roaming/Python/Scripts" >> $GITHUB_PATH
          else
            # For Unix
            curl -LsSf https://astral.sh/uv/install.sh | sh
            echo "$HOME/.cargo/bin" >> $GITHUB_PATH
          fi

      - name: Get version info
        id: version
        shell: bash
        run: |
          echo "date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT
          echo "short_sha=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

      - name: Build agfs-server
        working-directory: agfs-server
        env:
          GOOS: ${{ matrix.os }}
          GOARCH: ${{ matrix.arch }}
          CGO_ENABLED: 0
        run: |
          go build -ldflags="-s -w -X main.version=${{ steps.version.outputs.date }}-${{ steps.version.outputs.short_sha }}" -o ../build/agfs-server-${{ matrix.os }}-${{ matrix.arch }}${{ matrix.os == 'windows' && '.exe' || '' }} ./cmd/server

      - name: Build agfs-shell (portable)
        if: matrix.arch == 'amd64' || matrix.arch == 'arm64'
        shell: bash
        run: |
          cd agfs-shell

          # Find uv command
          if command -v uv &> /dev/null; then
            UV_CMD="uv"
          elif [ -f "$HOME/.cargo/bin/uv" ]; then
            UV_CMD="$HOME/.cargo/bin/uv"
          else
            echo "Error: uv not found"
            exit 1
          fi

          echo "Using uv: $UV_CMD"
          $UV_CMD --version

          # Sync dependencies
          $UV_CMD sync

          # Build portable distribution
          python3 build.py

          # Create archive name
          ARCHIVE_NAME="agfs-shell-${{ matrix.os }}-${{ matrix.arch }}"

          # Package the portable distribution
          if [ "${{ matrix.os }}" = "windows" ]; then
            # For Windows, create zip
            cd dist
            powershell Compress-Archive -Path agfs-shell-portable -DestinationPath "../../build/${ARCHIVE_NAME}.zip"
          else
            # For Unix, create tar.gz
            cd dist
            tar -czf "../../build/${ARCHIVE_NAME}.tar.gz" agfs-shell-portable/
          fi

      - name: Create archive (Unix)
        if: matrix.os != 'windows'
        working-directory: build
        run: |
          tar -czf agfs-${{ matrix.os }}-${{ matrix.arch }}-${{ steps.version.outputs.date }}.tar.gz agfs-server-${{ matrix.os }}-${{ matrix.arch }}*

      - name: Create archive (Windows)
        if: matrix.os == 'windows'
        working-directory: build
        shell: pwsh
        run: |
          $files = Get-ChildItem -Filter "agfs-*-${{ matrix.os }}-${{ matrix.arch }}*"
          Compress-Archive -Path $files -DestinationPath "agfs-${{ matrix.os }}-${{ matrix.arch }}-${{ steps.version.outputs.date }}.zip"

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: agfs-${{ matrix.os }}-${{ matrix.arch }}
          path: |
            build/agfs-${{ matrix.os }}-${{ matrix.arch }}-*.tar.gz
            build/agfs-${{ matrix.os }}-${{ matrix.arch }}-*.zip
            build/agfs-shell-${{ matrix.os }}-${{ matrix.arch }}.tar.gz
            build/agfs-shell-${{ matrix.os }}-${{ matrix.arch }}.zip
          retention-days: 90

  create-release:
    name: Create Daily Release
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'

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

      - name: Get version info
        id: version
        run: |
          echo "date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT
          echo "tag=nightly" >> $GITHUB_OUTPUT

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

      - name: Display structure of downloaded files
        run: ls -R release-artifacts

      - name: Prepare release assets
        run: |
          mkdir -p release
          find release-artifacts -type f \( -name "*.tar.gz" -o -name "*.zip" \) -exec cp {} release/ \;

      - name: Delete existing nightly release
        continue-on-error: true
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release delete nightly --yes --cleanup-tag

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: nightly
          name: Nightly Build (${{ steps.version.outputs.date }})
          body: |
            ## Daily Build - ${{ steps.version.outputs.date }}

            Automated daily build from commit ${{ github.sha }}

            ### 📦 What's Included

            This release contains:
            - **agfs-server**: Go binary (server)
            - **agfs-shell**: Python portable CLI with Unix-style pipeline support (requires Python 3.8+, includes all dependencies)

            ### Downloads

            #### Server Binaries

            - **Linux AMD64**: `agfs-linux-amd64-${{ steps.version.outputs.date }}.tar.gz`
            - **Linux ARM64**: `agfs-linux-arm64-${{ steps.version.outputs.date }}.tar.gz`
            - **macOS AMD64**: `agfs-darwin-amd64-${{ steps.version.outputs.date }}.tar.gz`
            - **macOS ARM64 (Apple Silicon)**: `agfs-darwin-arm64-${{ steps.version.outputs.date }}.tar.gz`

            #### CLI Client (Portable, Python 3.8+ required)

            - **Linux AMD64**: `agfs-shell-linux-amd64.tar.gz`
            - **Linux ARM64**: `agfs-shell-linux-arm64.tar.gz`
            - **macOS AMD64**: `agfs-shell-darwin-amd64.tar.gz`
            - **macOS ARM64**: `agfs-shell-darwin-arm64.tar.gz`

            ### Installation

            #### Quick Install (All-in-One)

            ```bash
            curl -fsSL https://raw.githubusercontent.com/c4pt0r/agfs/master/install.sh | sh
            ```

            This will install both server and client to `~/.local/bin/`.

            #### Manual Installation

            **Server (Linux/macOS):**
            ```bash
            # Extract
            tar -xzf agfs-<os>-<arch>-${{ steps.version.outputs.date }}.tar.gz

            # Make executable
            chmod +x agfs-server-<os>-<arch>

            # Move to bin directory
            mv agfs-server-<os>-<arch> ~/.local/bin/agfs-server

            # Run server
            agfs-server
            ```

            **Client (Linux/macOS):**
            ```bash
            # Extract
            tar -xzf agfs-shell-<os>-<arch>.tar.gz

            # Run directly
            ./agfs-shell-portable/agfs-shell

            # Or add to PATH
            export PATH=$PATH:$(pwd)/agfs-shell-portable
            ```

            ### Quick Start

            ```bash
            # Start the server
            agfs-server

            # In another terminal, use CLI with Unix-style pipelines
            agfs-shell
            # Then run commands like:
            # cat /etc/hosts | grep localhost
            # ls / | grep etc
            ```
          files: release/*
          draft: false
          prerelease: true
