name: Nix Build & Deploy

on:
  push:
    branches: ['develop']
  pull_request:
    branches: ['develop']

jobs:
  detect:
    runs-on: ubuntu-latest
    outputs:
      affected: ${{ steps.affected.outputs.apps }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install dependencies
        run: npm ci --legacy-peer-deps

      - name: Detect affected apps
        id: affected
        run: |
          AFFECTED=$(npx nx show projects --affected --base=HEAD~1 --type=app 2>/dev/null | tr '\n' ' ')
          echo "Affected apps: $AFFECTED"
          APPS="[]"
          for app in landing backoffice gacha dimentorin hackathon infra qrcampaign; do
            if echo "$AFFECTED" | grep -qw "$app"; then
              APPS=$(echo "$APPS" | jq -c ". + [\"$app\"]")
            fi
          done
          echo "apps=$APPS" >> "$GITHUB_OUTPUT"
          echo "Matrix: $APPS"

  build:
    needs: detect
    if: needs.detect.outputs.affected != '[]'
    runs-on: ubuntu-latest
    strategy:
      matrix:
        app: ${{ fromJson(needs.detect.outputs.affected) }}
    steps:
      - uses: actions/checkout@v4

      - name: Install Nix
        uses: DeterminateSystems/nix-installer-action@main

      - name: Setup Cachix
        uses: cachix/cachix-action@v15
        with:
          name: msdqn
          authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'

      - name: Build ${{ matrix.app }}
        run: nix build .#${{ matrix.app }} -o result-${{ matrix.app }}

      - name: Push to Cachix
        if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
        run: cachix push msdqn result-${{ matrix.app }}

  deploy:
    needs: build
    if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
    runs-on: ubuntu-latest
    steps:
      - name: Install Nix
        uses: DeterminateSystems/nix-installer-action@main

      - name: Setup SSH
        env:
          INFRA_DEPLOY_KEY: ${{ secrets.INFRA_DEPLOY_KEY }}
        run: |
          mkdir -p ~/.ssh
          echo "${INFRA_DEPLOY_KEY}" > ~/.ssh/deploy_key
          chmod 600 ~/.ssh/deploy_key
          ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null
          ssh-keyscan 167.235.70.37 >> ~/.ssh/known_hosts 2>/dev/null

      - name: Update infra flake.lock
        run: |
          export GIT_SSH_COMMAND="ssh -i ~/.ssh/deploy_key -o IdentitiesOnly=yes"
          git clone git@github.com:IMPHNEN/imphnen-infrastructure.git /tmp/infra
          cd /tmp/infra
          nix flake update imphnen-frontend
          if git diff --quiet flake.lock; then
            echo "flake.lock unchanged, skipping"
          else
            git config user.name "github-actions[bot]"
            git config user.email "github-actions[bot]@users.noreply.github.com"
            git add flake.lock
            git commit -m "chore: update imphnen-frontend-service to ${GITHUB_SHA::7}"
            git push
          fi

      - name: Deploy to server
        continue-on-error: true
        run: |
          ssh -i ~/.ssh/deploy_key -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new root@167.235.70.37 \
            'nixos-rebuild switch --flake github:IMPHNEN/imphnen-infrastructure#hetzner --refresh 2>&1 | tail -30'

      - name: Cleanup
        if: always()
        run: rm -f ~/.ssh/deploy_key
