#!/bin/bash

# Check for direct push to main
while read local_ref local_sha remote_ref remote_sha; do
    if [[ "$remote_ref" == "refs/heads/main" ]]; then
        echo -e "\e[31mERR:\e[0m Direct push to main is not allowed."
        echo -e "Please raise a PR and get it reviewed before merging into main.\n"
        exit 1
    fi
done

# Check branch name convention
BRANCH=$(git symbolic-ref --short HEAD)
PATTERN="^(feat|fix|chore|docs|refactor|revert)/.+"

if [[ ! "$BRANCH" =~ $PATTERN ]]; then
    echo -e "\e[31mERR:\e[0m Branch name '$BRANCH' does not follow naming convention."
    echo -e "Expected: <type>/<description>"
    echo -e "Example:  feat/add-vpc-module\n"
    exit 1
fi

# Check for pwsh
if ! command -v pwsh &>/dev/null; then
    echo -e "\e[31mERR:\e[0m PowerShell (pwsh) is not installed or not in PATH.\n"
    exit 1
fi

# Run workspace validation
if [ ! -f scripts/workspace-validate.ps1 ]; then
    echo -e "\e[31mERR:\e[0m scripts/workspace-validate.ps1 not found.\n"
    exit 1
fi

pwsh -NonInteractive -NoProfile -File scripts/workspace-validate.ps1
if [ $? -ne 0 ]; then
    echo -e "\e[31mERR:\e[0m Workspace validation failed. Fix the issues above before pushing.\n"
    exit 1
fi

exit 0