# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

name: Check the consistency of generated files

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  check-generated-files:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Install Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.25.x
          cache-dependency-path: "go.sum"

      - name: Set GOBIN
        run: echo "GOBIN=$HOME/.local/bin" >> $GITHUB_ENV

      - name: Add GOBIN to PATH
        run: echo "$HOME/.local/bin" >> $GITHUB_PATH

      - name: Check for changes in go.mod
        run: |
          go mod tidy
          git diff --exit-code

      - name: Check for changes in specific paths
        uses: dorny/paths-filter@v3
        id: changes
        with:
          base: main
          filters: |
            proto:
              - ".github/workflows/check-generated-files.yaml"
              - "auth.proto"
              - "auth/*.pb.go"
              - "pkg/messaging/message.proto"
              - "pkg/messaging/*.pb.go"

            mocks:
              - ".github/workflows/check-generated-files.yaml"
              - "pkg/sdk/sdk.go"
              - "users/postgres/clients.go"
              - "users/clients.go"
              - "pkg/clients/clients.go"
              - "pkg/messaging/pubsub.go"
              - "clients/postgres/clients.go"
              - "clients/clients.go"
              - "pkg/authz.go"
              - "pkg/authn.go"
              - "auth/domains.go"
              - "auth/keys.go"
              - "auth/service.go"
              - "pkg/events/events.go"
              - "pkg/groups/groups.go"
              - "users/emailer.go"
              - "users/hasher.go"
              - "certs/certs.go"
              - "certs/pki/openbao/openbao.go"
              - "certs/service.go"
              - "journal/journal.go"
              - "consumers/notifier.go"

      - name: Set up protoc
        if: steps.changes.outputs.proto == 'true'
        run: |
          PROTOC_VERSION=32.0
          PROTOC_GEN_VERSION=v1.36.8
          PROTOC_GRPC_VERSION=v1.5.1

          # Export the variables so they are available in future steps
          echo "PROTOC_VERSION=$PROTOC_VERSION" >> $GITHUB_ENV
          echo "PROTOC_GEN_VERSION=$PROTOC_GEN_VERSION" >> $GITHUB_ENV
          echo "PROTOC_GRPC_VERSION=$PROTOC_GRPC_VERSION" >> $GITHUB_ENV

          # Download and install protoc
          PROTOC_ZIP=protoc-$PROTOC_VERSION-linux-x86_64.zip
          curl -0L -o $PROTOC_ZIP https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOC_VERSION/$PROTOC_ZIP
          unzip -o $PROTOC_ZIP -d protoc3
          sudo mv protoc3/bin/* /usr/local/bin/
          sudo mv protoc3/include/* /usr/local/include/
          rm -rf $PROTOC_ZIP protoc3

          # Install protoc-gen-go and protoc-gen-go-grpc
          go install google.golang.org/protobuf/cmd/protoc-gen-go@$PROTOC_GEN_VERSION
          go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@$PROTOC_GRPC_VERSION

          # Add protoc to the PATH
          export PATH=$PATH:/usr/local/bin/protoc

      - name: Check Protobuf is up to Date
        if: steps.changes.outputs.proto == 'true'
        run: |
          for p in $(find . -name "*.pb.go"); do
            mv $p $p.tmp
          done

          make proto

          for p in $(find . -name "*.pb.go"); do
            if ! cmp -s $p $p.tmp; then
              echo "Error: Proto file and generated Go file $p are out of sync!"
              echo "Here is the difference:"
              diff $p $p.tmp || true
              echo "Please run 'make proto' with protoc version $PROTOC_VERSION, protoc-gen-go version $PROTOC_GEN_VERSION and protoc-gen-go-grpc version $PROTOC_GRPC_VERSION and commit the changes."
              exit 1
            fi
          done

      - name: Check Mocks are up to Date
        if: steps.changes.outputs.mocks == 'true'
        run: |
          for f in $(find . -type f -path '*/mocks/*.go' ! -name 'doc.go'); do
              mv $f $f.tmp
          done

          make mocks

          check_mock_changes() {
            local file_path=$1
            local tmp_file_path=$1.tmp
            local entity_name=$2

            if ! cmp -s "$file_path" "$tmp_file_path"; then
                echo "Error: Generated mocks for $entity_name are out of sync!"
                echo "Please run 'make mocks' with mockery version $MOCKERY_VERSION and commit the changes."
                exit 1
            fi
          }

          for f in $(find . -type f -path '*/mocks/*.go' ! -name 'doc.go'); do
              check_mock_changes $f "$f"
          done
