#!/usr/bin/env python3
"""
Count shlokas per section, chapter, and total
"""

import json
import re
from collections import defaultdict, Counter

with open('shlokas_export_2026-05-14_SORTED.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# Section names
SECTION_NAMES = {
    'SU': 'Sutrasthana',
    'NI': 'Nidanasthana',
    'VI': 'Vimanasthana',
    'SHA': 'Sharirasthana',
    'IN': 'Indriyasthana',
    'CHI': 'Cikitsasthana',
    'KAL': 'Kalpasthana',
    'SID': 'Siddhisthana'
}

# Group by section
by_section = defaultdict(list)
for item in data:
    sid = item['shloka_id']
    match = re.match(r'CH_([A-Z]+)', sid)
    if match:
        sec = match.group(1)
        by_section[sec].append(item)

print("=" * 60)
print("CHARAKA SAMHITA - SHLOKA COUNTS BY SECTION")
print("=" * 60)
print()

section_order = ['SU', 'NI', 'VI', 'SHA', 'IN', 'CHI', 'KAL', 'SID']
grand_total = 0

for sec in section_order:
    if sec not in by_section:
        continue

    items = by_section[sec]
    section_total = len(items)
    grand_total += section_total

    # Group by chapter
    chapters = defaultdict(list)
    for item in items:
        sid = item['shloka_id']
        match = re.match(r'CH_[A-Z]+_([\d.]+)', sid)
        if match:
            chap = match.group(1)
            chapters[chap].append(item)

    print(f"\n{SECTION_NAMES[sec]} ({sec}) - {len(chapters)} Chapters - {section_total} Shlokas")
    print("-" * 60)

    # Sort chapters
    def chap_sort_key(c):
        parts = c.split('.')
        return (int(parts[0]), int(parts[1]) if len(parts) > 1 else 0)

    for chap in sorted(chapters.keys(), key=chap_sort_key):
        chap_items = chapters[chap]
        print(f"  Chapter {chap}: {len(chap_items)} shlokas")

print()
print("=" * 60)
print(f"GRAND TOTAL: {grand_total} shlokas")
print("=" * 60)
