#!/usr/bin/env python3
import json
import os
import glob
from jsonschema import validate, ValidationError

def load_schema():
    """加载schema文件"""
    with open('knowledge_schema.json', 'r', encoding='utf-8') as f:
        return json.load(f)

def validate_json_file(file_path, schema):
    """验证单个JSON文件是否符合schema"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)

        validate(instance=data, schema=schema)
        return True, None
    except ValidationError as e:
        return False, e
    except json.JSONDecodeError as e:
        return False, f"JSON解析错误: {e}"
    except Exception as e:
        return False, f"其他错误: {e}"

def main():
    # 加载schema
    schema = load_schema()

    # 获取所有JSON文件（除了schema文件自身）
    json_files = glob.glob('knowledge-*.json')

    print(f"开始验证 {len(json_files)} 个JSON文件...")
    print("=" * 80)

    errors = []
    valid_files = []

    for file_path in sorted(json_files):
        is_valid, error = validate_json_file(file_path, schema)

        if is_valid:
            print(f"✓ {file_path} - 验证通过")
            valid_files.append(file_path)
        else:
            print(f"✗ {file_path} - 验证失败")
            if isinstance(error, ValidationError):
                errors.append({
                    'file': file_path,
                    'error': error,
                    'path': list(error.path) if error.path else [],
                    'message': error.message
                })
                print(f"    错误位置: {' -> '.join(map(str, error.path)) if error.path else '根级别'}")
                print(f"    错误信息: {error.message}")
            else:
                errors.append({
                    'file': file_path,
                    'error': str(error),
                    'path': [],
                    'message': str(error)
                })
                print(f"    错误信息: {error}")
            print()

    print("=" * 80)
    print(f"验证完成！")
    print(f"总文件数: {len(json_files)}")
    print(f"验证通过: {len(valid_files)}")
    print(f"验证失败: {len(errors)}")

    if errors:
        print("\n验证失败的文件详情:")
        for i, error in enumerate(errors, 1):
            print(f"\n{i}. 文件: {error['file']}")
            if error['path']:
                print(f"   位置: {' -> '.join(map(str, error['path']))}")
            print(f"   错误: {error['message']}")

    return len(errors) == 0

if __name__ == "__main__":
    success = main()
    exit(0 if success else 1)