name: CI - Main (API)

on:
  pull_request:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  release-please:
    name: Release Please
    # Only run release-please on pushes to main
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'

      - id: release
        uses: googleapis/release-please-action@v4
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    outputs:
      releases_created: ${{ steps.release.outputs.releases_created || 'false' }}
      tag_name: ${{ steps.release.outputs.tag_name || '' }}
  test-api:
    name: Test API
    defaults:
      run:
        working-directory: api
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@v1.4.3
        with:
          packages: bash procps python3 libvirt-dev jq zstd git build-essential
          version: 1.0

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: PNPM Install
        run: pnpm install --frozen-lockfile

      - name: Lint
        run: pnpm run lint

      - name: Test
        run: pnpm run coverage

  build-api:
    name: Build API
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: api
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@v1.4.3
        with:
          packages: bash procps python3 libvirt-dev jq zstd git build-essential
          version: 1.0

      - name: PNPM Install
        run: |
          cd ${{ github.workspace }}
          pnpm install --frozen-lockfile

      - name: Lint
        run: pnpm run lint

      - name: Type Check
        run: pnpm run type-check

      - name: Build
        run: pnpm run build

      - name: Get Git Short Sha and API version
        id: vars
        run: |
          GIT_SHA=$(git rev-parse --short HEAD)
          IS_TAGGED=$(git describe --tags --abbrev=0 --exact-match || echo '')
          PACKAGE_LOCK_VERSION=$(jq -r '.version' package.json)
          API_VERSION=$([[ -n "$IS_TAGGED" ]] && echo "$PACKAGE_LOCK_VERSION" || echo "${PACKAGE_LOCK_VERSION}+${GIT_SHA}")
          export API_VERSION

      - name: Build
        run: |
          pnpm run build:release

          tar -czf deploy/unraid-api.tgz -C deploy/pack/ .

      - name: Upload tgz to Github artifacts
        uses: actions/upload-artifact@v4
        with:
          name: unraid-api
          path: ${{ github.workspace }}/api/deploy/unraid-api.tgz

  build-unraid-ui-webcomponents:
    name: Build Unraid UI Library (Webcomponent Version)
    defaults:
      run:
        working-directory: unraid-ui
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@v1.4.3
        with:
          packages: bash procps python3 libvirt-dev jq zstd git build-essential
          version: 1.0

      - name: Install dependencies
        run: |
          cd ${{ github.workspace }}
          pnpm install --frozen-lockfile --filter @unraid/ui

      - name: Build
        run: pnpm run build:wc

      - name: Upload Artifact to Github
        uses: actions/upload-artifact@v4
        with:
          name: unraid-wc-ui
          path: unraid-ui/dist-wc/

  build-web:
    # needs: [build-unraid-ui]
    name: Build Web App
    defaults:
      run:
        working-directory: web
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Create env file
        run: |
          touch .env
          echo VITE_ACCOUNT=${{ secrets.VITE_ACCOUNT }} >> .env
          echo VITE_CONNECT=${{ secrets.VITE_CONNECT }} >> .env
          echo VITE_UNRAID_NET=${{ secrets.VITE_UNRAID_NET }} >> .env
          echo VITE_CALLBACK_KEY=${{ secrets.VITE_CALLBACK_KEY }} >> .env
          cat .env

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: PNPM Install
        run: |
          cd ${{ github.workspace }}
          pnpm install --frozen-lockfile --filter @unraid/web --filter @unraid/ui

      - name: Build Unraid UI
        run: |
          cd ${{ github.workspace }}/unraid-ui
          pnpm run build

      - name: Lint files
        run: pnpm run lint

      - name: Type Check
        run: pnpm run type-check

      - name: Test
        run: pnpm run test:ci

      - name: Build
        run: pnpm run build

      - name: Upload build to Github artifacts
        uses: actions/upload-artifact@v4
        with:
          name: unraid-wc-rich
          path: web/.nuxt/nuxt-custom-elements/dist/unraid-components

  build-plugin:
    name: Build and Deploy Plugin
    needs:
      - release-please
      - build-api
      - build-web
      - build-unraid-ui-webcomponents
      - test-api
    defaults:
      run:
        working-directory: plugin
    runs-on: ubuntu-latest
    steps:
      - name: Set Timezone
        uses: szenius/set-timezone@v2.0
        with:
          timezoneLinux: "America/Los_Angeles"
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version-file: ".nvmrc"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: Install dependencies
        run: |
          cd ${{ github.workspace }}
          pnpm install --frozen-lockfile --filter @unraid/connect-plugin

      - name: Download Unraid UI Components
        uses: actions/download-artifact@v4
        with:
          name: unraid-wc-ui
          path: ${{ github.workspace }}/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/unraid-components/uui
          merge-multiple: true
      - name: Download Unraid Web Components
        uses: actions/download-artifact@v4
        with:
          pattern: unraid-wc-rich
          path: ${{ github.workspace }}/plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/unraid-components/nuxt
          merge-multiple: true
      - name: Download Unraid API
        uses: actions/download-artifact@v4
        with:
          name: unraid-api
          path: ${{ github.workspace }}/plugin/api/
      - name: Extract Unraid API
        run: |
          mkdir -p ${{ github.workspace }}/plugin/source/dynamix.unraid.net/usr/local/unraid-api
          tar -xzf ${{ github.workspace }}/plugin/api/unraid-api.tgz -C ${{ github.workspace }}/plugin/source/dynamix.unraid.net/usr/local/unraid-api
      - name: Build Plugin and TXZ Based on Event and Tag
        id: build-plugin
        run: |
          cd ${{ github.workspace }}/plugin
          pnpm run build:txz

          if [ -n "${{ github.event.pull_request.number }}" ]; then
            TAG="PR${{ github.event.pull_request.number }}"
            BUCKET_PATH="unraid-api/tag/${TAG}"
          else
            TAG=""
            BUCKET_PATH="unraid-api"
          fi

          if [ "${{ needs.release-please.outputs.releases_created }}" == 'true' ]; then
            BASE_URL="https://stable.dl.unraid.net/unraid-api"
          else
            BASE_URL="https://preview.dl.unraid.net/unraid-api"
          fi

          echo "BUCKET_PATH=${BUCKET_PATH}" >> $GITHUB_OUTPUT
          echo "TAG=${TAG}" >> $GITHUB_OUTPUT

          pnpm run build:plugin --tag="${TAG}" --base-url="${BASE_URL}"

      - name: Ensure Plugin Files Exist
        run: |
          if [ ! -f ./deploy/*.plg ]; then
            echo "Error: .plg file not found in plugin/deploy/"
            exit 1
          fi

          if [ ! -f ./deploy/*.txz ]; then
            echo "Error: .txz file not found in plugin/deploy/"
            exit 1
          fi
      - name: Upload to GHA
        uses: actions/upload-artifact@v4
        with:
          name: unraid-plugin
          path: plugin/deploy/
      - name: Upload to Cloudflare
        if: github.event_name == 'pull_request' || startsWith(github.ref, 'refs/heads/main')
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: auto
        run: |
          # Sync the deploy directory to the Cloudflare bucket with explicit content encoding and public-read ACL
          aws s3 sync deploy/ s3://${{ secrets.CF_BUCKET_PREVIEW }}/${{ steps.build-plugin.outputs.BUCKET_PATH }} \
            --endpoint-url ${{ secrets.CF_ENDPOINT }} \
            --checksum-algorithm CRC32 \
            --no-guess-mime-type \
            --content-encoding none \
            --acl public-read

      - name: Upload Release Assets
        if: needs.release-please.outputs.releases_created == 'true'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          release_name=$(gh release list --repo ${{ github.repository }} --json name,isDraft --jq '.[] | select(.isDraft == true) | .name' | head -n 1)
          # For each file in release directory
          for file in deploy/*; do
            echo "Uploading $file to release..."
            gh release upload "${release_name}" "$file" --clobber
          done

      - name: Comment URL
        if: github.event_name == 'pull_request'
        uses: thollander/actions-comment-pull-request@v3
        with:
          comment-tag: prlink
          mode: recreate
          message: |
            This plugin has been deployed to Cloudflare R2 and is available for testing.
            Download it at this URL:
            ```
            https://preview.dl.unraid.net/unraid-api/tag/${{ steps.build-plugin.outputs.tag }}/dynamix.unraid.net.plg
            ```
