#!/bin/bash

echo "=========================================="
echo "Filament 开发规范快速检查工具"
echo "=========================================="
echo ""

ERRORS=0

# 检查 Filament 页面类
echo "1. 检查 Filament 页面类..."
grep -r "extends Component" app/Filament/Pages/ 2>/dev/null && {
    echo "   ❌ 错误：页面类不应继承 Component"
    ((ERRORS++))
} || {
    echo "   ✅ 正确：所有页面继承 Filament\Pages\Page"
}

# 检查属性类型声明
echo ""
echo "2. 检查页面属性类型声明..."
grep -r "navigationGroup.*string" app/Filament/Pages/ 2>/dev/null && {
    echo "   ❌ 错误：navigationGroup 类型应为 string|UnitEnum|null"
    ((ERRORS++))
} || {
    echo "   ✅ 正确：navigationGroup 类型正确"
}

# 检查视图文件根元素
echo ""
echo "3. 检查视图文件只有一个根元素..."
for file in resources/views/filament/pages/*.blade.php resources/views/livewire/*.blade.php; do
    if [ -f "$file" ]; then
        OPENING_TAGS=$(grep -c "^<[a-zA-Z]" "$file" 2>/dev/null || echo 0)
        CLOSING_TAGS=$(grep -c "^</" "$file" 2>/dev/null || echo 0)

        if [ $OPENING_TAGS -ne $CLOSING_TAGS ] || [ $OPENING_TAGS -ne 1 ]; then
            echo "   ❌ 错误：$file 根元素数量不正确"
            ((ERRORS++))
        else
            echo "   ✅ 正确：$file"
        fi
    fi
done

# 检查是否有脚本在外面
echo ""
echo "4. 检查是否有独立脚本标签在外面..."
SCRIPT_ERRORS=$(grep -A1 "^</div>" resources/views/filament/pages/*.blade.php resources/views/livewire/*.blade.php 2>/dev/null | grep "^<script\|^<style" | wc -l)
if [ $SCRIPT_ERRORS -gt 0 ]; then
    echo "   ❌ 错误：发现 $SCRIPT_ERRORS 个外部脚本/样式标签"
    ((ERRORS++))
else
    echo "   ✅ 正确：无外部脚本/样式标签"
fi

# 总结
echo ""
echo "=========================================="
if [ $ERRORS -eq 0 ]; then
    echo "✅ 所有检查通过！系统符合 Filament 规范"
    exit 0
else
    echo "❌ 发现 $ERRORS 个错误！请修复后重新检查"
    echo ""
    echo "参考文档："
    echo "  - /Users/yemeishu/.claude/FILAMENT_STRICT_DEVELOPMENT_RULES.md"
    echo "  - /Users/yemeishu/.claude/skills/README.md"
    exit 1
fi
