#!/bin/bash
# =============================================================================
# Naming Conventions Check
# Ensures code follows Classeo naming conventions
# =============================================================================

set -e

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

WARNINGS=0
ERRORS=0

echo "Checking naming conventions..."
echo ""

# =============================================================================
# PHP Backend Checks
# =============================================================================

echo "=== PHP Backend Checks ==="

# Check all PHP files have declare(strict_types=1) on line 1 or 3 (after opening tag)
echo "Checking strict_types declarations..."
for file in $(find backend/src -name "*.php" 2>/dev/null); do
    if ! grep -q "declare(strict_types=1)" "$file"; then
        echo -e "${RED}ERROR: Missing declare(strict_types=1) in $file${NC}"
        ERRORS=$((ERRORS + 1))
    fi
done

# Check that Value Objects are readonly
echo "Checking Value Objects are readonly..."
for file in $(find backend/src -path "*/Domain/Model/*Id.php" 2>/dev/null); do
    if ! grep -q "readonly class" "$file"; then
        echo -e "${YELLOW}WARNING: Value Object should be readonly: $file${NC}"
        WARNINGS=$((WARNINGS + 1))
    fi
done

# Check Domain Events naming (should end with past tense verbs)
echo "Checking Domain Event naming..."
for file in $(find backend/src -path "*/Domain/Event/*.php" 2>/dev/null); do
    filename=$(basename "$file" .php)
    # Events should end with past tense (ed, en, etc.)
    if [[ ! "$filename" =~ (ed|en|t|Created|Updated|Deleted|Recorded|Registered|Assigned|Completed)$ ]]; then
        echo -e "${YELLOW}WARNING: Domain Event should use past tense: $filename${NC}"
        WARNINGS=$((WARNINGS + 1))
    fi
done

# =============================================================================
# Frontend Checks
# =============================================================================

echo ""
echo "=== Frontend Checks ==="

# Check Svelte components are PascalCase
echo "Checking Svelte component naming..."
for file in $(find frontend/src -name "*.svelte" 2>/dev/null); do
    filename=$(basename "$file" .svelte)
    # Skip +page.svelte, +layout.svelte, +error.svelte
    if [[ "$filename" != +* ]]; then
        # Check first character is uppercase
        if [[ ! "$filename" =~ ^[A-Z] ]]; then
            echo -e "${YELLOW}WARNING: Svelte component should be PascalCase: $file${NC}"
            WARNINGS=$((WARNINGS + 1))
        fi
    fi
done

# Check for Svelte 4 patterns (forbidden)
echo "Checking for Svelte 4 patterns (should use Svelte 5 runes)..."
for file in $(find frontend/src -name "*.svelte" -o -name "*.ts" 2>/dev/null); do
    # Check for writable/readable stores (Svelte 4)
    if grep -q "from 'svelte/store'" "$file" 2>/dev/null; then
        echo -e "${RED}ERROR: Using svelte/store (Svelte 4) instead of runes: $file${NC}"
        ERRORS=$((ERRORS + 1))
    fi

    # Check for on:click (Svelte 4) instead of onclick (Svelte 5)
    if grep -q "on:click" "$file" 2>/dev/null; then
        echo -e "${RED}ERROR: Using on:click (Svelte 4) instead of onclick (Svelte 5): $file${NC}"
        ERRORS=$((ERRORS + 1))
    fi

    # Check for export let (Svelte 4) instead of $props (Svelte 5)
    if grep -q "export let" "$file" 2>/dev/null; then
        echo -e "${RED}ERROR: Using 'export let' (Svelte 4) instead of \$props (Svelte 5): $file${NC}"
        ERRORS=$((ERRORS + 1))
    fi
done

# =============================================================================
# Summary
# =============================================================================

echo ""
echo "=== Summary ==="

if [ $ERRORS -gt 0 ]; then
    echo -e "${RED}Naming check FAILED with $ERRORS error(s) and $WARNINGS warning(s)${NC}"
    exit 1
elif [ $WARNINGS -gt 0 ]; then
    echo -e "${YELLOW}Naming check PASSED with $WARNINGS warning(s)${NC}"
    exit 0
else
    echo -e "${GREEN}Naming check PASSED${NC}"
    exit 0
fi
