import os
import re
import csv
from pathlib import Path

base = Path(r'C:\Users\dfduq\OneDrive\Documents\ganglio sergioc')
rows = []
for md_path in sorted(base.rglob('*.md')):
    if md_path.parent.name == 'memory' or 'TRIAGE' in md_path.name or 'README' in md_path.name or 'INDICE' in md_path.name:
        continue
    rel = md_path.relative_to(base)
    folder = rel.parts[0] if len(rel.parts) > 1 else ''
    try:
        text = md_path.read_text(encoding='utf-8', errors='replace')
    except Exception as e:
        print(f'Err reading {md_path}: {e}')
        continue
    title_match = re.search(r'^#\s+(.+?)$', text, re.MULTILINE)
    title = title_match.group(1).strip() if title_match else md_path.stem
    pmid = re.search(r'\*\*PMID:\*\*\s*(\d+)', text)
    pmcid = re.search(r'\*\*PMCID:\*\*\s*(PMC\d+|—|-)', text)
    doi = re.search(r'\*\*DOI:\*\*\s*\[([^\]]+)\]', text)
    year = re.search(r'\*\*Revista:\*\*\s*[^()]+\((\d{4})\)', text)
    journal = re.search(r'\*\*Revista:\*\*\s*([^()]+?)\s*\(', text)
    authors = re.search(r'\*\*Autores:\*\*\s*([^\n]+)', text)
    source = re.search(r'\*\*Fuente:\*\*\s*([^\n]+)', text)
    rows.append({
        'folder': folder,
        'filename': md_path.name,
        'title': title[:200],
        'year': year.group(1) if year else '',
        'first_author': (authors.group(1).split(',')[0] if authors else '')[:80],
        'pmid': pmid.group(1) if pmid else '',
        'pmcid': pmcid.group(1) if pmcid else '',
        'doi': doi.group(1) if doi else '',
        'source': source.group(1)[:80] if source else '',
        'relative_path': str(rel).replace(os.sep, '/'),
    })

# Write CSV
csv_path = base / 'indice_metadatos' / 'INDICE_MAESTRO.csv'
with csv_path.open('w', newline='', encoding='utf-8') as f:
    w = csv.DictWriter(f, fieldnames=['folder','filename','title','year','first_author','pmid','pmcid','doi','source','relative_path'])
    w.writeheader()
    w.writerows(rows)

print(f'Total entries: {len(rows)}')

# Build Markdown index grouped by folder
md_index_path = base / 'indice_metadatos' / 'INDICE_MAESTRO.md'
folder_titles = {
    '01_revisiones_metaanalisis':'01 — Revisiones sistemáticas y meta-análisis',
    '02_ensayos_clinicos':'02 — Ensayos clínicos (pivotales y registros)',
    '03_cancer_mama_axila':'03 — Cáncer de mama (axila, general)',
    '04_otros_tumores':'04 — Otros tumores con conversión nodal',
    '05_tecnicas_quirurgicas_TAD_SLNB':'05 — Técnicas quirúrgicas (TAD, SLNB)',
    '06_imagen_y_marcaje':'06 — Imagen, marcaje y predicción',
    '07_anatomia_patologica':'07 — Anatomía patológica',
    '08_guias_clinicas':'08 — Guías clínicas',
    '09_radioterapia_post_NAC':'09 — Radioterapia adaptada post-NAC',
    '10_pronostico_supervivencia':'10 — Pronóstico y supervivencia',
}
by_folder = {}
for r in rows:
    by_folder.setdefault(r['folder'], []).append(r)

lines = [
    '# Índice Maestro — Repositorio Ganglio cN+ → ypN0 (Sergio Cervera)',
    '',
    f'**Total de entradas:** {len(rows)}',
    f'**Fecha de compilación:** 2026-05-12',
    '',
    '> Listado completo de artículos por carpeta. Click sobre el título para abrir el archivo Markdown con metadatos, abstract y (cuando disponible) full-text. DOIs enlazan al artículo original.',
    '',
    '## Estadísticas por carpeta',
    '',
    '| Carpeta | N |',
    '|---------|--:|',
]
for folder in sorted(by_folder.keys()):
    lines.append(f'| {folder_titles.get(folder, folder)} | {len(by_folder[folder])} |')
lines.append(f'| **TOTAL** | **{len(rows)}** |')
lines.append('')

for folder in sorted(by_folder.keys()):
    lines.append(f'## {folder_titles.get(folder, folder)}')
    lines.append('')
    items = sorted(by_folder[folder], key=lambda r: r.get('year') or '0000', reverse=True)
    for r in items:
        y = r['year'] or '----'
        author = r['first_author'] or '?'
        pmid_s = f"PMID {r['pmid']}" if r['pmid'] else ''
        pmcid_s = f" · {r['pmcid']}" if r['pmcid'] and r['pmcid'] not in ('—','-') else ''
        doi_s = f' · [DOI](https://doi.org/{r["doi"]})' if r['doi'] else ''
        path_md = r['relative_path']
        lines.append(f'- **{y}** · [{r["title"]}]({path_md}) — *{author}* · {pmid_s}{pmcid_s}{doi_s}')
    lines.append('')

md_index_path.write_text('\n'.join(lines), encoding='utf-8')
print(f'Wrote {md_index_path}')
print(f'Wrote {csv_path}')
