#!/bin/bash

# Color codes
red='\033[0;31m'
yellow='\033[0;33m'
blue='\033[0;34m'
green='\033[0;32m'
NC='\033[0m' # No colors

printf "\n"

if ! [[ $(cat "$1") =~ ^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-zA-Z0-9]+\))?:|Merge\ pull) ]]; then
  printf "  /‾‾‾‾‾‾‾‾\n"
  printf " <  Uh-oh! Please use semantic commit messages\n"
  printf "  \________\n\n"
  printf "  ${yellow}<type>${NC}[${green}<scope>${NC}]: ${blue}<short summary>${NC}\n"
  printf "${yellow}     │${green}      │             ${blue}│\n"
  printf "${yellow}     │${green}      │             ${blue}└─> Summary in present tense. Not capitalized. No period at the end. \n"
  printf "${yellow}     │${green}      │\n"
  printf "${yellow}     │${green}      └─> Scope: vite|authentication|core|\n"
  printf "${yellow}     │                                                                                          \n"
  printf "${yellow}     └─> Type: build, chore, ci, docs, feat, fix, perf, refactor, revert, style or test.\n"
  exit 1
fi

while read -r line; do
  # Skip comments
  if [ "${line:0:1}" == "#" ]; then
      continue
  fi
  if [ ${#line -gt 72} ]; then
    echo -e "${yellow}Commit messages are limited to 72 characters.${NC}"
    echo -e "The following commit message has ${red}${#line}${NC} characters."
    echo "${line}"
    exit 1
  fi
  done < "${1}"
exit 0

if [ "$branch" = "production" ]; then
    echo "You can't commit directly to the production branch"
    exit 1
fi

if [ "$branch" = "sandbox" ]; then
    echo "You can't commit directly to the sandbox branch"
    exit 1
fi
