name: Main

on:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    name: Test
    runs-on: namespace-profile-starterkit
    environment: staging
    steps:
      - name: Checkout repository
        uses: namespacelabs/nscloud-checkout-action@v5

      - name: Setup caches
        uses: namespacelabs/nscloud-cache-action@v1
        with:
          path: |
            .turbo
            ~/.bun/install/cache

      - name: Start Docker Compose
        run: docker compose up -d

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version-file: .bun-version

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install Foundry
        uses: foundry-rs/foundry-toolchain@v1
        with:
          version: stable

      - name: Install Bun dependencies
        run: bun install

      - name: Install Playwright Browsers
        run: bunx playwright install --with-deps

      - name: Connect to SettleMint
        uses: settlemint/settlemint-action@main
        with:
          instance: ${{ secrets.SETTLEMINT_INSTANCE }}
          access-token: ${{ secrets.SETTLEMINT_PERSONAL_ACCESS_TOKEN }}
          dotEnvFile: ${{ secrets.ENV_FILE }}
          dotEnvLocalFile: ${{ secrets.ENV_FILE_LOCAL }}

      - name: Turbo!
        run: bunx turbo dependencies lint build test genesis abi-output

      - name: Store predeployed output
        uses: actions/upload-artifact@v4
        with:
          name: predeployed-output
          path: |
            kit/contracts/genesis-output.json
            kit/subgraph/subgraph-output.txt
            kit/contracts/portal/*.json

      - name: Turbo!
        run: bunx turbo deploy:local

  security:
    name: Security
    runs-on: namespace-profile-starterkit
    steps:
      - name: Checkout repository
        uses: namespacelabs/nscloud-checkout-action@v5

      - name: Secret Scanning (Trivy)
        uses: aquasecurity/trivy-action@0.29.0
        with:
          scan-type: 'fs'
          scan-ref: '.'
          trivy-config: trivy-secret.yaml
          severity: "HIGH,CRITICAL"
          format: 'table'
          exit-code: '1'
          scanners: 'secret'

  custom-deployment:
    name: Custom Deployment dAPP
    runs-on: namespace-profile-starterkit
    environment: staging
    needs:
      - security
      - test
    steps:
      - name: Checkout repository
        uses: namespacelabs/nscloud-checkout-action@v5

      - name: Setup caches
        uses: namespacelabs/nscloud-cache-action@v1
        with:
          path: |
            .turbo
            ~/.bun/install/cache

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version-file: .bun-version

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Install Foundry
        uses: foundry-rs/foundry-toolchain@v1
        with:
          version: stable

      - name: Install Bun dependencies
        run: bun install

      - name: Connect to SettleMint
        uses: settlemint/settlemint-action@main
        with:
          instance: ${{ secrets.SETTLEMINT_INSTANCE }}
          access-token: ${{ secrets.SETTLEMINT_PERSONAL_ACCESS_TOKEN }}
          dotEnvFile: ${{ secrets.ENV_FILE }}
          dotEnvLocalFile: ${{ secrets.ENV_FILE_LOCAL }}

      - name: Turbo!
        run: bunx turbo codegen addresses

      - name: Inject slug/short variables
        uses: rlespinasse/github-slug-action@v5

      - name: Update package versions
        id: package-version
        run: bash .github/scripts/update-version.sh

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Docker Metadata
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            ghcr.io/settlemint/starterkit-asset-tokenization
          tags: |
            type=raw,value=${{ env.VERSION }}

      - name: Extract SHA tag
        id: sha-tag
        run: |
          echo "sha_tag=$(echo "${{ steps.meta.outputs.tags }}" | grep "sha-" | head -n1)" >> $GITHUB_OUTPUT

      - name: Build and Push
        uses: docker/build-push-action@v6
        with:
          context: kit/dapp
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          platforms: linux/amd64,linux/arm64
          provenance: mode=max
          sbom: true

      - name: Run Drizzle migrations
        run: bunx drizzle-kit push --force
        working-directory: kit/dapp

      - name: Update Custom Deployment
        uses: settlemint/settlemint-action@main
        with:
          auto-connect: false
          access-token: ${{ secrets.SETTLEMINT_ACCESS_TOKEN }}
          dotEnvFile: ${{ secrets.ENV_FILE }}
          dotEnvLocalFile: ${{ secrets.ENV_FILE_LOCAL }}
          command: platform update custom-deployment ${{ env.VERSION }} --wait
