name: Deploy

on:
  push:
    branches: [main]
    tags: ['v*']

env:
  REGISTRY: git.holo-grid.bar
  IMAGE_TONGO: git.holo-grid.bar/costap/quarkshologridledger/tongo-core
  IMAGE_WEB: git.holo-grid.bar/costap/quarkshologridledger/web-terminal

jobs:
  build-and-push:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v4

      - name: Set image tag
        run: |
          if [ "${{ github.ref_type }}" = "tag" ]; then
            echo "TAG=${{ github.ref_name }}" >> $GITHUB_ENV
          else
            echo "TAG=sha-$(git rev-parse --short HEAD)" >> $GITHUB_ENV
          fi

      - name: Login to Forgejo Registry
        run: echo "${{ secrets.REGISTRY_TOKEN }}" | docker login $REGISTRY -u ${{ secrets.REGISTRY_USER }} --password-stdin

      - name: Build & Push Tongo Core
        run: |
          docker build -t $IMAGE_TONGO:$TAG -t $IMAGE_TONGO:latest -f tongo-core/Dockerfile .
          docker push $IMAGE_TONGO:$TAG
          docker push $IMAGE_TONGO:latest

      - name: Build & Push Web Terminal
        run: |
          docker build -t $IMAGE_WEB:$TAG -t $IMAGE_WEB:latest -f web-terminal/Dockerfile .
          docker push $IMAGE_WEB:$TAG
          docker push $IMAGE_WEB:latest

  deploy:
    runs-on: self-hosted
    needs: build-and-push
    steps:
      - uses: actions/checkout@v4

      - name: Set environment target variables
        id: target
        run: |
          if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
            echo "env_name=production" >> $GITHUB_OUTPUT
            echo "deploy_host=${{ secrets.DEPLOY_HOST_PROD }}" >> $GITHUB_OUTPUT
            echo "deploy_key<<EOF" >> $GITHUB_OUTPUT
            echo "${{ secrets.DEPLOY_SSH_KEY_PROD }}" >> $GITHUB_OUTPUT
            echo "EOF" >> $GITHUB_OUTPUT
          else
            echo "env_name=staging" >> $GITHUB_OUTPUT
            echo "deploy_host=${{ secrets.DEPLOY_HOST_STAGING }}" >> $GITHUB_OUTPUT
            echo "deploy_key<<EOF" >> $GITHUB_OUTPUT
            echo "${{ secrets.DEPLOY_SSH_KEY_STAGING }}" >> $GITHUB_OUTPUT
            echo "EOF" >> $GITHUB_OUTPUT
          fi

      - name: Deploy to Hetzner target environment
        env:
          ENV_NAME: ${{ steps.target.outputs.env_name }}
          DEPLOY_HOST: ${{ steps.target.outputs.deploy_host }}
          DEPLOY_KEY: ${{ steps.target.outputs.deploy_key }}
        run: |
          mkdir -p ~/.ssh
          echo "$DEPLOY_KEY" > ~/.ssh/deploy_key
          chmod 600 ~/.ssh/deploy_key

          # Copy production compose file and configuration
          scp -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no \
            deploy/docker-compose.prod.yml \
            deploy/Caddyfile \
            deploy@$DEPLOY_HOST:/opt/hologrid/

          # Pull new images and restart the stack
          ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no deploy@$DEPLOY_HOST << 'EOF'
            cd /opt/hologrid
            echo "${{ secrets.REGISTRY_TOKEN }}" | docker login ${{ env.REGISTRY }} -u ${{ secrets.REGISTRY_USER }} --password-stdin
            docker compose -f docker-compose.prod.yml pull
            docker compose -f docker-compose.prod.yml up -d --remove-orphans
            docker image prune -f
          EOF
