import os
import json

def find_adi_fields(json_path):
    """Ищет поля с именем 'adı' или 'adi'"""
    try:
        with open(json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        if 'fields' not in data:
            return None
        
        found_fields = []
        for field in data['fields']:
            if 'fieldname' in field:
                fn = field['fieldname']
                # Ищем точное совпадение с 'adi' или 'adı'
                if fn in ['adi', 'adı']:
                    found_fields.append(fn)
        
        if len(found_fields) >= 2:  # Найдены оба
            return {
                'doctype': data.get('name', 'Unknown'),
                'file': json_path,
                'fields': found_fields
            }
        
        return None
    except Exception as e:
        return None

# Путь к приложению
app_path = '/home/frappe/frappe-bench/apps/taxes_az/taxes_az/taxes_az'
doctype_path = os.path.join(app_path, 'doctype')

if not os.path.exists(doctype_path):
    print(f"❌ Путь не найден: {doctype_path}")
    exit(1)

print("=" * 60)
print("Поиск DocType с полями 'adi' И 'adı'")
print("=" * 60 + "\n")

problems = []

for root, dirs, files in os.walk(doctype_path):
    for file in files:
        if file.endswith('.json'):
            json_path = os.path.join(root, file)
            result = find_adi_fields(json_path)
            if result:
                problems.append(result)

if problems:
    print(f"✓ Найдено {len(problems)} DocType с проблемой:\n")
    for p in problems:
        print(f"DocType: {p['doctype']}")
        print(f"Файл: {p['file']}")
        print(f"Найденные поля: {p['fields']}")
        print(f"➜ Один из них нужно переименовать (рекомендую 'adi' → 'adi_select')")
        print()
else:
    print("✓ Проблем не найдено! Все DocType в порядке.")