#!/bin/bash

# Pattern Library Validation Script
# Validates pattern library JSON structure and quality

set -e  # Exit on error

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

# Default pattern library path
PATTERN_LIB="${1:-pattern_library/patterns.json}"

echo "=================================="
echo "Pattern Library Validation Script"
echo "=================================="
echo ""

# Check if file exists
if [ ! -f "$PATTERN_LIB" ]; then
    echo -e "${RED}ERROR: Pattern library not found at: $PATTERN_LIB${NC}"
    exit 1
fi

echo -e "Validating: ${GREEN}$PATTERN_LIB${NC}"
echo ""

# Check if JSON is valid
echo "1. Validating JSON syntax..."
if jq empty "$PATTERN_LIB" 2>/dev/null; then
    echo -e "   ${GREEN}✓ Valid JSON${NC}"
else
    echo -e "   ${RED}✗ Invalid JSON syntax${NC}"
    exit 1
fi

# Check required top-level fields
echo "2. Checking required fields..."
REQUIRED_FIELDS=("version" "last_updated" "total_iterations_analyzed" "patterns" "metadata")
for field in "${REQUIRED_FIELDS[@]}"; do
    if jq -e ".$field" "$PATTERN_LIB" >/dev/null 2>&1; then
        echo -e "   ${GREEN}✓ Field '$field' exists${NC}"
    else
        echo -e "   ${RED}✗ Missing required field: '$field'${NC}"
        exit 1
    fi
done

# Check pattern categories
echo "3. Checking pattern categories..."
CATEGORIES=("structural" "content" "innovation" "quality")
for category in "${CATEGORIES[@]}"; do
    if jq -e ".patterns.$category" "$PATTERN_LIB" >/dev/null 2>&1; then
        count=$(jq ".patterns.$category | length" "$PATTERN_LIB")
        echo -e "   ${GREEN}✓ Category '$category': $count patterns${NC}"

        # Validate pattern count (should be 3-5 for 'deep' analysis)
        if [ "$count" -lt 0 ] || [ "$count" -gt 5 ]; then
            echo -e "   ${YELLOW}⚠ Warning: Unexpected pattern count for '$category' (expected 0-5, got $count)${NC}"
        fi
    else
        echo -e "   ${RED}✗ Missing pattern category: '$category'${NC}"
        exit 1
    fi
done

# Check pattern object structure
echo "4. Validating pattern objects..."
PATTERN_FIELDS=("name" "description" "example_file" "key_characteristics" "success_metrics")
error_count=0

for category in "${CATEGORIES[@]}"; do
    pattern_count=$(jq ".patterns.$category | length" "$PATTERN_LIB")

    if [ "$pattern_count" -gt 0 ]; then
        for ((i=0; i<pattern_count; i++)); do
            pattern_name=$(jq -r ".patterns.$category[$i].name" "$PATTERN_LIB")
            echo -e "   Checking pattern: ${YELLOW}$category[$i] - $pattern_name${NC}"

            for field in "${PATTERN_FIELDS[@]}"; do
                if ! jq -e ".patterns.$category[$i].$field" "$PATTERN_LIB" >/dev/null 2>&1; then
                    echo -e "     ${RED}✗ Missing field '$field' in $category[$i]${NC}"
                    ((error_count++))
                fi
            done
        done
    fi
done

if [ $error_count -eq 0 ]; then
    echo -e "   ${GREEN}✓ All pattern objects valid${NC}"
else
    echo -e "   ${RED}✗ Found $error_count errors in pattern objects${NC}"
    exit 1
fi

# Check metadata
echo "5. Checking metadata..."
METADATA_FIELDS=("extraction_date" "source_directory" "iterations_count" "patterns_extracted")
for field in "${METADATA_FIELDS[@]}"; do
    if jq -e ".metadata.$field" "$PATTERN_LIB" >/dev/null 2>&1; then
        value=$(jq -r ".metadata.$field" "$PATTERN_LIB")
        echo -e "   ${GREEN}✓ metadata.$field = $value${NC}"
    else
        echo -e "   ${YELLOW}⚠ Optional field missing: metadata.$field${NC}"
    fi
done

# Calculate total patterns
echo "6. Calculating statistics..."
total_patterns=0
for category in "${CATEGORIES[@]}"; do
    count=$(jq ".patterns.$category | length" "$PATTERN_LIB")
    total_patterns=$((total_patterns + count))
done

version=$(jq -r ".version" "$PATTERN_LIB")
iterations=$(jq -r ".total_iterations_analyzed" "$PATTERN_LIB")
last_updated=$(jq -r ".last_updated" "$PATTERN_LIB")

echo -e "   ${GREEN}Version:${NC} $version"
echo -e "   ${GREEN}Total patterns:${NC} $total_patterns"
echo -e "   ${GREEN}Iterations analyzed:${NC} $iterations"
echo -e "   ${GREEN}Last updated:${NC} $last_updated"

# Validate pattern count consistency
declared_count=$(jq -r ".metadata.patterns_extracted" "$PATTERN_LIB")
if [ "$declared_count" != "null" ] && [ "$total_patterns" -ne "$declared_count" ]; then
    echo -e "   ${YELLOW}⚠ Warning: Pattern count mismatch (counted: $total_patterns, declared: $declared_count)${NC}"
fi

# Check for duplicate pattern names
echo "7. Checking for duplicate pattern names..."
all_pattern_names=$(jq -r '[.patterns[][].name] | sort' "$PATTERN_LIB")
unique_names=$(jq -r '[.patterns[][].name] | unique | sort' "$PATTERN_LIB")

if [ "$all_pattern_names" = "$unique_names" ]; then
    echo -e "   ${GREEN}✓ No duplicate pattern names${NC}"
else
    echo -e "   ${YELLOW}⚠ Warning: Duplicate pattern names detected${NC}"
fi

# Check pattern quality
echo "8. Assessing pattern quality..."
patterns_with_snippets=0
patterns_with_metrics=0
total_patterns_checked=0

for category in "${CATEGORIES[@]}"; do
    pattern_count=$(jq ".patterns.$category | length" "$PATTERN_LIB")

    for ((i=0; i<pattern_count; i++)); do
        ((total_patterns_checked++))

        # Check for code snippet
        if jq -e ".patterns.$category[$i].code_snippet" "$PATTERN_LIB" >/dev/null 2>&1; then
            snippet=$(jq -r ".patterns.$category[$i].code_snippet" "$PATTERN_LIB")
            if [ "$snippet" != "null" ] && [ -n "$snippet" ]; then
                ((patterns_with_snippets++))
            fi
        fi

        # Check for success metrics
        if jq -e ".patterns.$category[$i].success_metrics" "$PATTERN_LIB" >/dev/null 2>&1; then
            metrics=$(jq -r ".patterns.$category[$i].success_metrics" "$PATTERN_LIB")
            if [ "$metrics" != "null" ] && [ -n "$metrics" ]; then
                ((patterns_with_metrics++))
            fi
        fi
    done
done

if [ $total_patterns_checked -gt 0 ]; then
    snippet_percent=$((patterns_with_snippets * 100 / total_patterns_checked))
    metrics_percent=$((patterns_with_metrics * 100 / total_patterns_checked))

    echo -e "   ${GREEN}Patterns with code snippets:${NC} $patterns_with_snippets/$total_patterns_checked ($snippet_percent%)"
    echo -e "   ${GREEN}Patterns with success metrics:${NC} $patterns_with_metrics/$total_patterns_checked ($metrics_percent%)"

    if [ $snippet_percent -ge 80 ] && [ $metrics_percent -ge 80 ]; then
        echo -e "   ${GREEN}✓ High quality pattern library${NC}"
    else
        echo -e "   ${YELLOW}⚠ Consider adding more code snippets and success metrics${NC}"
    fi
fi

# Final summary
echo ""
echo "=================================="
echo "Validation Summary"
echo "=================================="
echo -e "${GREEN}✓ Pattern library is valid${NC}"
echo ""
echo "File: $PATTERN_LIB"
echo "Version: $version"
echo "Total patterns: $total_patterns"
echo "Quality score: $snippet_percent% complete"
echo ""
echo -e "${GREEN}Pattern library ready for use in infinite-synthesis command!${NC}"

exit 0
