#!/bin/bash

# Installation Test Script
# Verifies that the Pattern Synthesis system is correctly installed

set -e  # Exit on error

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

echo "================================================"
echo "Pattern Synthesis System - Installation Test"
echo "================================================"
echo ""

# Test counter
tests_passed=0
tests_failed=0

# Test function
run_test() {
    local test_name="$1"
    local test_command="$2"

    echo -n "Testing: $test_name ... "

    if eval "$test_command" > /dev/null 2>&1; then
        echo -e "${GREEN}✓ PASS${NC}"
        ((tests_passed++))
        return 0
    else
        echo -e "${RED}✗ FAIL${NC}"
        ((tests_failed++))
        return 1
    fi
}

# 1. Check directory structure
echo "1. Checking Directory Structure"
echo "================================"

run_test "Commands directory exists" "test -d .claude/commands"
run_test "Specs directory exists" "test -d specs"
run_test "Validators directory exists" "test -d validators"
run_test "Pattern library directory exists" "test -d pattern_library"

echo ""

# 2. Check command files
echo "2. Checking Command Files"
echo "========================="

run_test "infinite-synthesis.md exists" "test -f .claude/commands/infinite-synthesis.md"
run_test "extract-patterns.md exists" "test -f .claude/commands/extract-patterns.md"
run_test "analyze-patterns.md exists" "test -f .claude/commands/analyze-patterns.md"
run_test "settings.json exists" "test -f .claude/settings.json"

echo ""

# 3. Check specification files
echo "3. Checking Specification Files"
echo "================================"

run_test "example_spec.md exists" "test -f specs/example_spec.md"
run_test "example_spec.md not empty" "test -s specs/example_spec.md"

echo ""

# 4. Check documentation files
echo "4. Checking Documentation"
echo "========================="

run_test "README.md exists" "test -f README.md"
run_test "CLAUDE.md exists" "test -f CLAUDE.md"
run_test "EXAMPLES.md exists" "test -f EXAMPLES.md"
run_test "ARCHITECTURE.md exists" "test -f ARCHITECTURE.md"
run_test "QUICKSTART.md exists" "test -f QUICKSTART.md"
run_test "CHANGELOG.md exists" "test -f CHANGELOG.md"

echo ""

# 5. Check validator files
echo "5. Checking Validators"
echo "======================"

run_test "check_patterns.sh exists" "test -f validators/check_patterns.sh"
run_test "check_patterns.sh executable" "test -x validators/check_patterns.sh"

echo ""

# 6. Check template files
echo "6. Checking Templates"
echo "====================="

run_test "pattern_library_template.json exists" "test -f pattern_library_template.json"

# Validate template JSON if jq is available
if command -v jq &> /dev/null; then
    run_test "pattern_library_template.json valid JSON" "jq empty pattern_library_template.json"
else
    echo -e "${YELLOW}⚠ Skipping JSON validation (jq not installed)${NC}"
fi

echo ""

# 7. Check dependencies
echo "7. Checking Dependencies"
echo "========================"

if command -v jq &> /dev/null; then
    echo -e "jq (JSON processor): ${GREEN}✓ Installed${NC}"
    jq --version
    ((tests_passed++))
else
    echo -e "jq (JSON processor): ${RED}✗ Not Installed${NC}"
    echo "  Install: sudo apt-get install jq (Ubuntu) or brew install jq (macOS)"
    ((tests_failed++))
fi

echo ""

# 8. Validate pattern template
echo "8. Validating Pattern Template"
echo "==============================="

if command -v jq &> /dev/null; then
    if ./validators/check_patterns.sh pattern_library_template.json > /tmp/validation_output.txt 2>&1; then
        echo -e "${GREEN}✓ Pattern template validates successfully${NC}"
        ((tests_passed++))
    else
        echo -e "${RED}✗ Pattern template validation failed${NC}"
        echo "See /tmp/validation_output.txt for details"
        ((tests_failed++))
    fi
else
    echo -e "${YELLOW}⚠ Skipping validation (jq not installed)${NC}"
fi

echo ""

# 9. Check file permissions
echo "9. Checking File Permissions"
echo "============================="

run_test "Validator script is executable" "test -x validators/check_patterns.sh"
run_test "Test script is executable" "test -x test_installation.sh"

echo ""

# 10. Verify content completeness
echo "10. Verifying Content Completeness"
echo "===================================="

# Check that command files have content
run_test "infinite-synthesis.md has content" "test \$(wc -l < .claude/commands/infinite-synthesis.md) -gt 100"
run_test "extract-patterns.md has content" "test \$(wc -l < .claude/commands/extract-patterns.md) -gt 100"
run_test "analyze-patterns.md has content" "test \$(wc -l < .claude/commands/analyze-patterns.md) -gt 100"

# Check that docs have content
run_test "README.md has content" "test \$(wc -l < README.md) -gt 100"
run_test "CLAUDE.md has content" "test \$(wc -l < CLAUDE.md) -gt 100"

echo ""

# Summary
echo "================================================"
echo "Test Summary"
echo "================================================"
echo -e "Tests Passed: ${GREEN}$tests_passed${NC}"
echo -e "Tests Failed: ${RED}$tests_failed${NC}"
echo ""

if [ $tests_failed -eq 0 ]; then
    echo -e "${GREEN}✓ All tests passed! Installation is complete.${NC}"
    echo ""
    echo "Next steps:"
    echo "1. Start Claude Code: claude"
    echo "2. Run first generation: /project:infinite-synthesis specs/example_spec.md output 5"
    echo "3. Read QUICKSTART.md for detailed walkthrough"
    echo ""
    exit 0
else
    echo -e "${RED}✗ Some tests failed. Please fix the issues above.${NC}"
    echo ""
    echo "Common fixes:"
    echo "1. Install jq: sudo apt-get install jq (Ubuntu) or brew install jq (macOS)"
    echo "2. Make scripts executable: chmod +x validators/*.sh *.sh"
    echo "3. Check file paths match installation directory"
    echo ""
    exit 1
fi
