name: Build Dashboard Image

on:
  push:
    branches: [main]
    paths:
      - "dashboard/**"
      - "src/dashboard/**"
      - "pyproject.toml"
      - ".github/workflows/docker-build-dashboard.yml"
      - ".github/actions/ecr-build/**"
  workflow_dispatch:

permissions:
  id-token: write
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Generate CI app token
        id: app-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - uses: actions/checkout@v4

      - name: Build and push dashboard image
        id: build
        uses: ./.github/actions/ecr-build
        with:
          image_name: cogent
          dockerfile: dashboard/Dockerfile
          context: .
          aws_role: ${{ vars.AWS_ROLE }}
          tag_prefix: dashboard

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Build frontend tarball
        run: |
          cd dashboard/frontend
          npm ci
          npx next build
          mkdir -p /tmp/frontend-pkg
          cp -a .next/standalone/. /tmp/frontend-pkg/
          [ -d .next/static ] && mkdir -p /tmp/frontend-pkg/.next && cp -r .next/static /tmp/frontend-pkg/.next/static
          [ -d public ] && cp -r public /tmp/frontend-pkg/public
          tar czf /tmp/frontend.tar.gz -C /tmp/frontend-pkg .
          echo "SIZE=$(du -h /tmp/frontend.tar.gz | cut -f1)" >> "$GITHUB_ENV"

      - name: Upload frontend tarball to S3
        uses: ./.github/actions/s3-upload
        with:
          aws_role: ${{ vars.AWS_ROLE }}
          source_path: /tmp/frontend.tar.gz
          s3_key: dashboard/${{ github.sha }}/frontend.tar.gz

      - name: Summary
        run: |
          echo "### Dashboard Built" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "**Docker Tags:**" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"
          echo "${{ steps.build.outputs.image_uri }}" >> "$GITHUB_STEP_SUMMARY"
          echo '```' >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "**Frontend Tarball:** \`s3://cogent-polis-ci-artifacts/dashboard/${{ github.sha }}/frontend.tar.gz\` (${{ env.SIZE }})" >> "$GITHUB_STEP_SUMMARY"
          echo "" >> "$GITHUB_STEP_SUMMARY"
          echo "**Deploy:** \`cogos <name> cogtainer update dashboard --sha ${{ steps.build.outputs.short_sha }}\`" >> "$GITHUB_STEP_SUMMARY"

      - name: Update versions defaults (dashboard)
        uses: ./.github/actions/update-versions
        with:
          component: dashboard
          version: ${{ steps.build.outputs.short_sha }}
          token: ${{ steps.app-token.outputs.token }}

      - name: Update versions defaults (dashboard_frontend)
        uses: ./.github/actions/update-versions
        with:
          component: dashboard_frontend
          version: ${{ github.sha }}
          token: ${{ steps.app-token.outputs.token }}
