#!/usr/bin/env bash

# Containers to check (Later we should add the BI container)
CONTAINERS=("docker-backoffice-marketplace-1")

# Go to the project root
cd "$(git rev-parse --show-toplevel)"

# Check if terminal supports colors
if [ -t 1 ]; then
  RED='\033[0;31m'
  GREEN='\033[0;32m'
  NC='\033[0m'
else
  RED=''
  GREEN=''
  NC=''
fi

# Track statuses
PHPSTAN_STATUS=0
RECTOR_STATUS=0

for CONTAINER_NAME in "${CONTAINERS[@]}"; do
  echo -e "\n🔍 Running ${GREEN}PHPStan inside $CONTAINER_NAME${NC}..."
  docker exec -i "$CONTAINER_NAME" php vendor/bin/phpstan analyse
  if [ $? -ne 0 ]; then PHPSTAN_STATUS=1; fi

  echo -e "\n🔍 Running ${GREEN}Rector (dry-run) inside $CONTAINER_NAME${NC}..."
  docker exec -i "$CONTAINER_NAME" php vendor/bin/rector process --dry-run
  if [ $? -ne 0 ]; then RECTOR_STATUS=1; fi
done


if [ $PHPSTAN_STATUS -ne 0 ]; then
  echo -e "${RED}❌ Commit blocked: PHPStan found errors.${NC}"
fi

if [ $RECTOR_STATUS -ne 0 ]; then
   echo -e "${RED}❌ Commit blocked: Rector found issues in the code.${NC}"
fi

if [ $PHPSTAN_STATUS -ne 0 ] || [ $RECTOR_STATUS -ne 0 ]; then
  exit 1
fi

echo -e "${GREEN}✅ All checks passed. Proceeding with commit.${NC}"
exit 0