name: Build and Deploy to Production
run-name: Deploying commit ${{ gitea.sha_short }} by @${{ gitea.actor }}
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: website-deploy-runner

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Create Backend .env file
        run: |
          echo "Creating backend .env file..."
          cd backend
          # Using both Gitea Variables (vars) and Secrets (secrets)
          echo "BACKEND_PORT=${{ vars.BACKEND_PORT }}" >> .env
          echo "FRONTEND_URL=${{ vars.FRONTEND_URL }}" >> .env
          echo "SMTP_HOST=${{ vars.SMTP_HOST }}" >> .env
          echo "SMTP_PORT=${{ vars.SMTP_PORT }}" >> .env
          echo "SMTP_SECURE=${{ vars.SMTP_SECURE }}" >> .env
          echo "YOUR_RECEIVING_EMAIL=${{ vars.YOUR_RECEIVING_EMAIL }}" >> .env
          echo "SMTP_FROM_EMAIL=${{ vars.SMTP_FROM_EMAIL }}" >> .env

          # Secrets should be used for sensitive data
          echo "SMTP_USER=${{ secrets.SMTP_USER }}" >> .env
          echo "SMTP_PASS=${{ secrets.SMTP_PASS }}" >> .env


      - name: Create Frontend .env.local file
        run: |
          echo "Creating frontend .env.local file..."
          cd frontend
          # The BACKEND_URL is not needed because Traefik will handle routing /api
          echo "NEXT_PUBLIC_GITHUB_URL=${{ vars.NEXT_PUBLIC_GITHUB_URL }}" >> .env.local
          echo "NEXT_PUBLIC_LINKEDIN_URL=${{ vars.NEXT_PUBLIC_LINKEDIN_URL }}" >> .env.local

      - name: Cache Dependencies and Build Artifacts
        uses: actions/cache@v4
        with:
          path: |
            backend/node_modules
            frontend/node_modules
            frontend/.next/cache
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install Dependencies and Build
        run: |
          echo "Installing backend dependencies..."
          cd backend && npm install && cd ..
          echo "Installing frontend dependencies..."
          cd frontend && npm install
          echo "Building frontend application..."
          npm run build

      - name: Sync Files to Production Directory
        run: |
          rsync -a --delete --exclude 'frontend/.next/cache/' --exclude '.git/' ./ /var/www/website.joaoloureiro.dev.br/

      - name: Restart Applications with PM2
        env:
          DEPLOY_PATH: ${{ vars.DEPLOY_PATH }}
        run: |
          restart-portfolio

