name: Deploy Nyla Frontend to Integration

on:
  push:
    branches:
      - int
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Copy integration environment file
        run: |
          cp config/.env.int .env

      - name: Install dependencies
        run: |
          npm ci
          npm install express

      - name: Build React app for integration
        run: npm run build:int

      - name: Prepare deployment package
        run: |
          # Create deployment package with build files and necessary configs
          mkdir deploy
          cp -r dist/* deploy/
          # Create a simple server.js for serving the app
          echo "const express = require('express');" > deploy/server.js
          echo "const path = require('path');" >> deploy/server.js
          echo "const app = express();" >> deploy/server.js
          echo "app.use(express.static(path.join(__dirname)));" >> deploy/server.js
          echo "app.get('/*', function(req, res) { res.sendFile(path.join(__dirname, 'index.html')); });" >> deploy/server.js
          echo "const port = process.env.PORT || 8080;" >> deploy/server.js
          echo "app.listen(port, () => console.log('Server running on port', port));" >> deploy/server.js
          # Create a new package.json for deployment
          echo '{
            "name": "frontend-int",
            "version": "1.0.0",
            "private": true,
            "scripts": {
              "start": "node server.js"
            },
            "dependencies": {
              "express": "^4.18.2"
            }
          }' > deploy/package.json

      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v3
        with:
          app-name: 'poweron-nyla-int'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_POWERON_NYLA_INT }}
          package: ./deploy 