#!/bin/bash

# Check if plugin is 100% compliant

echo "=== COMMENTS PRESS ZONE PLUGIN COMPLIANCE CHECK ==="
echo ""

# 1. Prefix Check
echo "1. PREFIX CHECK:"
prefix_count=$(grep -r "presszone_comments_" includes/ --include="*.php" | wc -l)
if [ $prefix_count -gt 0 ]; then
    echo "   ✓ Prefixes are correct (presszone_comments_)"
else
    echo "   ✗ Prefixes are missing or incorrect"
fi

echo ""

# 2. Text Domain Check
echo "2. TEXT DOMAIN CHECK:"
textdomain_count=$(grep -r "comments-press-zone" includes/ --include="*.php" | wc -l)
if [ $textdomain_count -gt 0 ]; then
    echo "   ✓ Text domain is correct (comments-press-zone)"
else
    echo "   ✗ Text domain is missing or incorrect"
fi

echo ""

# 3. CSS Custom Properties Check
echo "3. CSS CUSTOM PROPERTIES CHECK:"
css_custom_properties_count=$(grep -r "var(--presszone-comments-" assets/ --include="*.scss" | wc -l)
if [ $css_custom_properties_count -eq 0 ]; then
    echo "   ✓ No CSS custom properties found (uses SCSS variables)"
else
    echo "   ✗ CSS custom properties found (should use SCSS variables)"
fi

echo ""

# 4. SQL Injection Prevention Check
echo "4. SQL INJECTION PREVENTION CHECK:"
sql_prepare_count=$(grep -r "\$wpdb->prepare" includes/ --include="*.php" | wc -l)
if [ $sql_prepare_count -gt 0 ]; then
    echo "   ✓ SQL queries use $wpdb->prepare()"
else
    echo "   ✗ SQL queries missing $wpdb->prepare()"
fi

echo ""

# 5. Output Escaping Check
echo "5. OUTPUT ESCAPING CHECK:"
output_escaping_count=$(grep -r "esc_html\\|esc_attr\\|wp_kses_post" includes/ --include="*.php" | wc -l)
if [ $output_escaping_count -gt 0 ]; then
    echo "   ✓ Output is properly escaped"
else
    echo "   ✗ Output escaping missing"
fi

echo ""

# 6. Input Sanitization Check
echo "6. INPUT SANITIZATION CHECK:"
input_sanitization_count=$(grep -r "sanitize_text_field\\|absint\\|map_deep" includes/ --include="*.php" | wc -l)
if [ $input_sanitization_count -gt 0 ]; then
    echo "   ✓ Input is properly sanitized"
else
    echo "   ✗ Input sanitization missing"
fi

echo ""

# 7. Nonce Verification Check
echo "7. NONCE VERIFICATION CHECK:"
nonce_count=$(grep -r "check_ajax_referer\\|wp_verify_nonce" includes/ --include="*.php" | wc -l)
if [ $nonce_count -gt 0 ]; then
    echo "   ✓ Nonces are verified"
else
    echo "   ✗ Nonce verification missing"
fi

echo ""

# 8. Permission Checks Check
echo "8. PERMISSION CHECKS CHECK:"
permission_count=$(grep -r "current_user_can\\|is_user_logged_in" includes/ --include="*.php" | wc -l)
if [ $permission_count -gt 0 ]; then
    echo "   ✓ Permission checks are in place"
else
    echo "   ✗ Permission checks missing"
fi

echo ""

# 9. Accessibility Check
echo "9. ACCESSIBILITY CHECK:"
accessibility_count=$(grep -r "aria-label\\|aria-expanded\\|role=" templates/ --include="*.php" | wc -l)
if [ $accessibility_count -gt 0 ]; then
    echo "   ✓ Accessibility attributes are present"
else
    echo "   ✗ Accessibility attributes missing"
fi

echo ""

# 10. Skip Links Check
echo "10. SKIP LINKS CHECK:"
skip_links_count=$(grep -r "skip.*link" templates/ --include="*.php" | wc -l)
if [ $skip_links_count -gt 0 ]; then
    echo "   ✓ Skip links are present"
else
    echo "   ✗ Skip links missing"
fi

echo ""

# 11. REST API Security Check
echo "11. REST API SECURITY CHECK:"
rest_security_count=$(grep -r "__return_true" includes/Api/ --include="*.php" | wc -l)
if [ $rest_security_count -eq 0 ]; then
    echo "   ✓ REST API endpoints have proper permission callbacks"
else
    echo "   ✗ REST API endpoints have __return_true callbacks (security risk)"
fi

echo ""

# Summary
echo "=== COMPLIANCE SUMMARY ==="

# Count total checks passed
passed_checks=0
failed_checks=0

if [ $prefix_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $textdomain_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $css_custom_properties_count -eq 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $sql_prepare_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $output_escaping_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $input_sanitization_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $nonce_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $permission_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $accessibility_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $skip_links_count -gt 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

if [ $rest_security_count -eq 0 ]; then
    ((passed_checks++))
else
    ((failed_checks++))
fi

echo "   ✓ Passed: $passed_checks"
echo "   ✗ Failed: $failed_checks"
echo ""

if [ $failed_checks -eq 0 ]; then
    echo "✅ PLUGIN IS 100% COMPLIANT!"
else
    echo "❌ PLUGIN IS NOT 100% COMPLIANT. $failed_checks issues need to be fixed."
fi