#!/bin/bash

# Quick LESS/SCSS compilation test
# Tests that LESS and SCSS files compile without errors and produce similar output

set -e

cd "$(dirname "$0")"

echo "==========================================="
echo "Quick LESS/SCSS Compilation Test"
echo "==========================================="
echo ""

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Test LESS compilation
printf "${YELLOW}Testing LESS compilation...${NC}\n"
npx lessc main.less /tmp/test-main-less.css 2>/tmp/lessc-errors.log
if [ $? -eq 0 ]; then
  LESS_SIZE=$(wc -c < /tmp/test-main-less.css | tr -d ' ')
  LESS_SIZE_MB=$(echo "scale=1; $LESS_SIZE / 1024 / 1024" | bc)
  printf "${GREEN}✓ LESS compilation successful${NC} (${LESS_SIZE_MB}M)\n"
else
  printf "${RED}✗ LESS compilation failed${NC}\n"
  cat /tmp/lessc-errors.log
  exit 1
fi

# Test SCSS compilation
printf "${YELLOW}Testing SCSS compilation...${NC}\n"
npx sass main.scss /tmp/test-main-scss.css --no-source-map 2>/tmp/sass-errors.log
if [ $? -eq 0 ]; then
  SCSS_SIZE=$(wc -c < /tmp/test-main-scss.css | tr -d ' ')
  SCSS_SIZE_MB=$(echo "scale=1; $SCSS_SIZE / 1024 / 1024" | bc)
  printf "${GREEN}✓ SCSS compilation successful${NC} (${SCSS_SIZE_MB}M)\n"
else
  printf "${RED}✗ SCSS compilation failed${NC}\n"
  cat /tmp/sass-errors.log
  exit 1
fi

# Compare sizes (should be within 5%)
SIZE_DIFF=$(echo "scale=2; ($LESS_SIZE - $SCSS_SIZE) / $SCSS_SIZE * 100" | bc)
SIZE_DIFF_ABS=$(echo "$SIZE_DIFF" | tr -d '-')

echo ""
echo "==========================================="
printf "${YELLOW}Comparison:${NC}\n"
echo "  LESS: ${LESS_SIZE_MB}M"
echo "  SCSS: ${SCSS_SIZE_MB}M"
echo "  Difference: ${SIZE_DIFF}%"
echo "  Difference absolute: ${SIZE_DIFF_ABS}%"

if [ "$(echo "$SIZE_DIFF_ABS < 5" | bc -l)" -eq 1 ]; then
  printf "${GREEN}✓ Output sizes match (within 5%%)${NC}\n"
else
  printf "${RED}✗ Warning: Output sizes differ by more than 5%%${NC}\n"
fi

echo "==========================================="
echo ""
echo "Detailed warnings/deprecations:"
echo ""
cat /tmp/lessc-errors.log | head -20
echo ""
echo "Full logs: /tmp/lessc-errors.log, /tmp/sass-errors.log"
