"""
Check JSON Format

Check what format JSONLoader returns.
"""

from services.json_loader import JSONLoader
from services.settings_service import SettingsService
import json

def check_json_format():
    """Check JSON format."""
    print("=" * 80)
    print("JSON Format Check")
    print("=" * 80)
    
    settings_service = SettingsService()
    config = settings_service.config
    
    loader = JSONLoader(
        config['json_sources']['base_path'],
        config['collections']
    )
    
    # Load records
    records = loader.load_collection_from_source('odf_rankings', 'ba_glf_2018')
    
    print(f"\nTotal records: {len(records)}")
    
    if records:
        print("\nFirst record keys (first 20):")
        first_record = records[0]
        for key in list(first_record.keys())[:20]:
            print(f"  {key}: {first_record[key]}")
        
        print(f"\nTotal keys in first record: {len(first_record.keys())}")
        
        # Check for business key fields
        business_keys = ['competitor.code', 'odf_body.competition_code', 'rank']
        print(f"\nBusiness key fields:")
        for key in business_keys:
            value = first_record.get(key)
            print(f"  {key}: {value}")
        
        # Check if data is already flattened
        has_dots = any('.' in k for k in first_record.keys())
        print(f"\nData is flattened (has dot notation): {has_dots}")
        
        # Show sample
        print(f"\nSample record (formatted):")
        print(json.dumps(first_record, indent=2, default=str)[:500])

if __name__ == "__main__":
    try:
        check_json_format()
    except Exception as e:
        print(f"\n❌ Error: {e}")
        import traceback
        traceback.print_exc()
