#!/bin/bash
# ============================================
# PharmData Design System - Color Validation
# ============================================
#
# Validates that no hardcoded colors are used in the codebase.
# Run this before commits to prevent dark mode regressions.
#
# Usage:
#   ./scripts/validate-colors.sh [--fix]
#
# Options:
#   --fix    Show suggested replacements (does not auto-fix)
#

set -e

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

ERRORS=0
WARNINGS=0
PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"

echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}  PharmData Design System - Color Validation${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""

# ============================================
# Check 1: Hardcoded white backgrounds
# ============================================
echo -e "${YELLOW}[1/4]${NC} Checking for hardcoded backgrounds..."

HARDCODED_BG=$(grep -rn \
    -e "background:\s*white" \
    -e "background:\s*#fff" \
    -e "background:\s*#ffffff" \
    -e "background-color:\s*white" \
    -e "background-color:\s*#fff" \
    -e "background-color:\s*#ffffff" \
    --include="*.html" --include="*.css" \
    "$PROJECT_ROOT" 2>/dev/null \
    | grep -v "node_modules" \
    | grep -v "dark-mode" \
    | grep -v "\.md:" \
    | grep -v "validate-colors" \
    || true)

if [ -n "$HARDCODED_BG" ]; then
    echo -e "${RED}  ✗ Found hardcoded backgrounds:${NC}"
    echo "$HARDCODED_BG" | while read -r line; do
        echo -e "    ${RED}→${NC} $line"
    done
    echo ""
    echo -e "    ${YELLOW}Fix:${NC} Replace with var(--surface-primary) or var(--surface-elevated)"
    echo ""
    ERRORS=$((ERRORS + 1))
else
    echo -e "${GREEN}  ✓ No hardcoded backgrounds found${NC}"
fi

# ============================================
# Check 2: Tailwind built-in color classes
# ============================================
echo -e "${YELLOW}[2/4]${NC} Checking for Tailwind built-in colors..."

TAILWIND_BUILTIN=$(grep -rn \
    -e "bg-white" \
    -e "bg-gray-" \
    -e "bg-slate-" \
    -e "bg-zinc-" \
    -e "bg-neutral-" \
    -e "bg-stone-" \
    -e "text-gray-" \
    -e "text-slate-" \
    -e "text-zinc-" \
    -e "text-neutral-" \
    -e "text-stone-" \
    -e "border-gray-" \
    -e "border-slate-" \
    --include="*.html" \
    "$PROJECT_ROOT" 2>/dev/null \
    | grep -v "node_modules" \
    | grep -v "dark-mode" \
    | grep -v "\.md:" \
    | grep -v "validate-colors" \
    || true)

if [ -n "$TAILWIND_BUILTIN" ]; then
    echo -e "${RED}  ✗ Found Tailwind built-in color classes:${NC}"
    echo "$TAILWIND_BUILTIN" | head -20 | while read -r line; do
        echo -e "    ${RED}→${NC} $line"
    done

    COUNT=$(echo "$TAILWIND_BUILTIN" | wc -l | tr -d ' ')
    if [ "$COUNT" -gt 20 ]; then
        echo -e "    ${YELLOW}... and $((COUNT - 20)) more${NC}"
    fi

    echo ""
    echo -e "    ${YELLOW}Fix:${NC} Replace bg-white → bg-surface, text-gray-* → text-text"
    echo ""
    ERRORS=$((ERRORS + 1))
else
    echo -e "${GREEN}  ✓ No Tailwind built-in colors found${NC}"
fi

# ============================================
# Check 3: Inline hex colors in style attributes
# ============================================
echo -e "${YELLOW}[3/4]${NC} Checking for inline hex colors..."

INLINE_HEX=$(grep -rn \
    'style="[^"]*#[0-9a-fA-F]\{3,6\}' \
    --include="*.html" \
    "$PROJECT_ROOT" 2>/dev/null \
    | grep -v "node_modules" \
    | grep -v "\.md:" \
    | grep -v "validate-colors" \
    || true)

if [ -n "$INLINE_HEX" ]; then
    echo -e "${YELLOW}  ⚠ Found inline hex colors (review manually):${NC}"
    echo "$INLINE_HEX" | head -10 | while read -r line; do
        echo -e "    ${YELLOW}→${NC} $line"
    done

    COUNT=$(echo "$INLINE_HEX" | wc -l | tr -d ' ')
    if [ "$COUNT" -gt 10 ]; then
        echo -e "    ${YELLOW}... and $((COUNT - 10)) more${NC}"
    fi
    echo ""
    WARNINGS=$((WARNINGS + 1))
else
    echo -e "${GREEN}  ✓ No inline hex colors found${NC}"
fi

# ============================================
# Check 4: Duplicate dark mode variable definitions
# ============================================
echo -e "${YELLOW}[4/4]${NC} Checking for duplicate dark mode definitions..."

DARK_MODE_FILES=$(find "$PROJECT_ROOT" -name "*.css" -exec grep -l "dark-mode" {} \; 2>/dev/null \
    | grep -v "node_modules" \
    | grep -v "validate-colors" \
    || true)

DARK_MODE_COUNT=$(echo "$DARK_MODE_FILES" | grep -c "" || echo 0)

if [ "$DARK_MODE_COUNT" -gt 2 ]; then
    echo -e "${YELLOW}  ⚠ Dark mode variables defined in multiple files:${NC}"
    echo "$DARK_MODE_FILES" | while read -r file; do
        if [ -n "$file" ]; then
            echo -e "    ${YELLOW}→${NC} $file"
        fi
    done
    echo ""
    echo -e "    ${YELLOW}Recommendation:${NC} Consolidate to tokens/colors.css only"
    echo ""
    WARNINGS=$((WARNINGS + 1))
else
    echo -e "${GREEN}  ✓ Dark mode definitions are consolidated${NC}"
fi

# ============================================
# Summary
# ============================================
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "${BLUE}  Summary${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"

if [ $ERRORS -eq 0 ] && [ $WARNINGS -eq 0 ]; then
    echo -e "${GREEN}  ✓ All validation checks passed!${NC}"
    echo ""
    exit 0
elif [ $ERRORS -eq 0 ]; then
    echo -e "${YELLOW}  ⚠ Passed with $WARNINGS warning(s)${NC}"
    echo ""
    exit 0
else
    echo -e "${RED}  ✗ Failed with $ERRORS error(s) and $WARNINGS warning(s)${NC}"
    echo ""
    echo -e "  Run with ${YELLOW}--fix${NC} to see replacement suggestions."
    echo ""
    exit 1
fi
