#!/usr/bin/env python3
"""
检查Repository层命名规范
"""

import os
import re
from pathlib import Path

def check_repository_naming():
    """检查Repository层命名规范"""
    base_path = Path("/Users/zhangxiang/Codes/Novalon/novalon-manage-system/novalon-manage-api")
    
    print("=" * 60)
    print("Repository层命名规范检查")
    print("=" * 60)
    
    # 查找所有Repository接口
    repository_interfaces = []
    for java_file in base_path.rglob("*Repository.java"):
        content = java_file.read_text()
        if "interface" in content:
            repository_interfaces.append(java_file)
    
    print(f"\n找到 {len(repository_interfaces)} 个Repository接口:")
    
    issues = []
    for interface in sorted(repository_interfaces):
        interface_name = interface.stem
        content = interface.read_text()
        
        # 检查命名规范
        if interface_name.startswith('I'):
            print(f"  ✅ {interface_name}")
        else:
            print(f"  ⚠️  {interface_name} (应该以I开头)")
            issues.append((interface, interface_name, f"I{interface_name}"))
    
    # 查找所有Repository实现类
    repository_impls = []
    for java_file in base_path.rglob("*Repository*.java"):
        if "impl" in str(java_file) or "RepositoryImpl" in java_file.name:
            content = java_file.read_text()
            if "class" in content and "Repository" in content:
                repository_impls.append(java_file)
    
    print(f"\n找到 {len(repository_impls)} 个Repository实现类:")
    
    for impl in sorted(repository_impls):
        impl_name = impl.stem
        content = impl.read_text()
        
        # 检查是否实现了接口
        implements_match = re.search(r'implements\s+(\w+)', content)
        if implements_match:
            interface_name = implements_match.group(1)
            
            # 检查命名规范
            if interface_name.startswith('I'):
                expected_impl_name = interface_name[1:]  # 移除I前缀
                
                if impl_name == expected_impl_name:
                    print(f"  ✅ {impl_name} implements {interface_name}")
                else:
                    print(f"  ⚠️  {impl_name} implements {interface_name}")
                    print(f"      建议重命名为: {expected_impl_name}")
                    issues.append((impl, impl_name, expected_impl_name))
            else:
                print(f"  ℹ️  {impl_name} implements {interface_name} (非标准接口)")
        else:
            print(f"  ❓ {impl_name} (未找到implements关键字)")
    
    # 检查是否有不符合规范的命名
    print("\n" + "=" * 60)
    if issues:
        print(f"发现 {len(issues)} 个命名不规范的问题:")
        for file, current_name, expected_name in issues:
            print(f"  - {current_name} -> {expected_name}")
            print(f"    文件: {file.relative_to(base_path)}")
    else:
        print("✅ 所有Repository命名都符合规范！")
    
    print("=" * 60)

if __name__ == "__main__":
    check_repository_naming()
