name: Build & Deploy OS League Tools

on:
  push:
    branches:
      - main

env:
  FRONTEND_HOST: psg-leagues-tools-frontend

jobs:
  build-and-deploy:
    runs-on: ["cicd"]
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set environment variables
        run: |
          if [ "${{ github.ref_name }}" = "main" ]; then
            echo "DEPLOY_PATH=/home/sonder/leagues-tools/os-league-tools-master" >> $GITHUB_ENV
            echo "SERVICE_NAME=os-league-tools" >> $GITHUB_ENV
            echo "ENVIRONMENT=production" >> $GITHUB_ENV
          else
            echo "DEPLOY_PATH=/home/sonder/leagues-tools-dev/os-league-tools-master" >> $GITHUB_ENV
            echo "SERVICE_NAME=os-league-tools-dev" >> $GITHUB_ENV
            echo "ENVIRONMENT=development" >> $GITHUB_ENV
          fi

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: os-league-tools-master/package-lock.json

      - name: Install dependencies
        working-directory: os-league-tools-master
        run: npm ci

      - name: Build production bundle
        working-directory: os-league-tools-master
        run: npm run build
        env:
          CI: false

      - name: Archive build
        working-directory: os-league-tools-master
        run: tar -czf build.tar.gz build/

      - name: Setup SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -H ${{ env.FRONTEND_HOST }} >> ~/.ssh/known_hosts

      - name: Deploy to frontend server
        run: |
          echo "Deploying ${{ env.ENVIRONMENT }} to ${{ env.DEPLOY_PATH }}"
          ssh sonder@${{ env.FRONTEND_HOST }} "sudo systemctl stop ${{ env.SERVICE_NAME }} || true"

          if [ "${{ env.ENVIRONMENT }}" = "production" ]; then
            # Production: deploy only built files
            ssh sonder@${{ env.FRONTEND_HOST }} "mkdir -p ${{ env.DEPLOY_PATH }}"
            ssh sonder@${{ env.FRONTEND_HOST }} "rm -rf ${{ env.DEPLOY_PATH }}/build"
            scp os-league-tools-master/build.tar.gz sonder@${{ env.FRONTEND_HOST }}:/tmp/
            ssh sonder@${{ env.FRONTEND_HOST }} "tar -xzf /tmp/build.tar.gz -C ${{ env.DEPLOY_PATH }}/"
            ssh sonder@${{ env.FRONTEND_HOST }} "rm /tmp/build.tar.gz"
          else
            # Dev: deploy full source for hot reload
            ssh sonder@${{ env.FRONTEND_HOST }} "mkdir -p /home/sonder/leagues-tools-dev"
            tar -czf source.tar.gz os-league-tools-master/
            scp source.tar.gz sonder@${{ env.FRONTEND_HOST }}:/tmp/
            ssh sonder@${{ env.FRONTEND_HOST }} "rm -rf /home/sonder/leagues-tools-dev/os-league-tools-master"
            ssh sonder@${{ env.FRONTEND_HOST }} "tar -xzf /tmp/source.tar.gz -C /home/sonder/leagues-tools-dev/"
            ssh sonder@${{ env.FRONTEND_HOST }} "cd /home/sonder/leagues-tools-dev/os-league-tools-master && npm ci"
            ssh sonder@${{ env.FRONTEND_HOST }} "rm /tmp/source.tar.gz"
          fi

          ssh sonder@${{ env.FRONTEND_HOST }} "sudo systemctl start ${{ env.SERVICE_NAME }}"
          echo "Deployed ${{ env.ENVIRONMENT }} successfully!"
