name: Deploy DeepDrftPublic

on:
  push:
    branches: [master, dev]
    paths:
      - 'DeepDrftPublic/**'
      - 'DeepDrftPublic.Client/**'
      - 'DeepDrftShared.Client/**'
      - 'DeepDrftModels/**'
      - '.gitea/workflows/deploy-public.yml'

jobs:
  build:
    name: Build & Publish
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '10.0.x'

      - uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install wasm-tools workload
        run: dotnet workload install wasm-tools

      # No test project scoped to DeepDrftPublic — add one and wire it here when ready.

      - name: Publish (self-contained linux-x64)
        run: |
          dotnet publish DeepDrftPublic/DeepDrftPublic.csproj \
            -c Release \
            -r linux-x64 \
            --self-contained \
            -o DeepDrftPublic/publish

      - name: Package
        run: tar -czf deepdrft-public.tar.gz -C DeepDrftPublic/publish .

      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: deepdrft-public
          path: deepdrft-public.tar.gz
          retention-days: 1

  deploy:
    name: Deploy
    needs: build
    runs-on: ubuntu-24.04
    if: github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/dev')
    env:
      DEPLOY_HOST: ${{ github.ref == 'refs/heads/master' && 'prod.cerebellumsoftworks.com' || 'dch7.cerebellumsoftworks.com' }}
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v3
        with:
          name: deepdrft-public

      - name: Install SSH tooling
        run: apt-get update && apt-get install -y --no-install-recommends openssh-client rsync

      - name: Configure SSH
        env:
          PROD_KEY: ${{ secrets.DEEPDRFT_PROD_SSH_DEPLOY }}
          BETA_KEY: ${{ secrets.DEEPDRFT_DCH7_SSH_DEPLOY }}
        run: |
          mkdir -p ~/.ssh
          if [ "$DEPLOY_HOST" = "prod.cerebellumsoftworks.com" ]; then
            echo "$PROD_KEY" > ~/.ssh/deepdrft_ed25519
          else
            echo "$BETA_KEY" > ~/.ssh/deepdrft_ed25519
          fi
          chmod 600 ~/.ssh/deepdrft_ed25519
          ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts || { echo "ssh-keyscan failed for $DEPLOY_HOST"; exit 1; }

      - name: Rsync archive to staging
        run: |
          rsync -e "ssh -i ~/.ssh/deepdrft_ed25519 -o StrictHostKeyChecking=yes" \
            deepdrft-public.tar.gz \
            deepdrft@$DEPLOY_HOST:

      - name: Trigger deploy on host
        run: |
          ssh -i ~/.ssh/deepdrft_ed25519 -o StrictHostKeyChecking=yes \
            deepdrft@$DEPLOY_HOST deploy-public
