import json
import os
import re
import shutil
from pathlib import Path
import fitz

BASE = Path('/home/spazio/cosmos_book_extract')
SRC_FILES = [
    Path('/home/spazio/.hermes/cache/documents/doc_c86c6c128301_Дамский_салон,_часть_1_Гэри_Дуглас.pdf'),
    Path('/home/spazio/.hermes/cache/documents/doc_1944ea8fa2e8_Дамский_салон,_часть_2_Гэри_Дуглас.pdf'),
    Path('/home/spazio/.hermes/cache/documents/doc_118fab4e61db_Дамский_салон,_часть_3_Гэри_Дуглас.pdf'),
]

PDF_DIR = BASE / 'pdf'
TEXT_DIR = BASE / 'text'
CHUNK_DIR = BASE / 'daily_fragments'
for d in [PDF_DIR, TEXT_DIR, CHUNK_DIR]:
    d.mkdir(parents=True, exist_ok=True)

manifest = []
full_parts = []

for idx, src in enumerate(SRC_FILES, start=1):
    if not src.exists():
        raise FileNotFoundError(src)
    dest = PDF_DIR / f'damskiy_salon_part_{idx}.pdf'
    shutil.copy2(src, dest)
    doc = fitz.open(dest)
    pages = []
    nonempty = 0
    for pno, page in enumerate(doc, start=1):
        text = page.get_text('text') or ''
        text = text.replace('\x00', '').strip()
        if text:
            nonempty += 1
        pages.append(f'\n\n===== ЧАСТЬ {idx} · СТРАНИЦА {pno} =====\n\n{text}')
    text_all = '\n'.join(pages).strip()
    text_path = TEXT_DIR / f'part_{idx}.txt'
    text_path.write_text(text_all, encoding='utf-8')
    full_parts.append(f'\n\n########## ДАМСКИЙ САЛОН · ЧАСТЬ {idx} ##########\n\n{text_all}')
    manifest.append({
        'part': idx,
        'source_cache': str(src),
        'pdf_path': str(dest),
        'text_path': str(text_path),
        'pages': len(doc),
        'nonempty_pages': nonempty,
        'chars': len(text_all),
        'metadata': {k: v for k, v in doc.metadata.items() if v},
    })
    doc.close()

full_text = '\n'.join(full_parts).strip()
full_path = TEXT_DIR / 'damskiy_salon_all_parts.txt'
full_path.write_text(full_text, encoding='utf-8')

# Split by approximate daily reading chunks, preserving page markers.
# Target ~16k chars to fit daily summary while moving steadily.
page_blocks = re.split(r'(?=\n\n===== ЧАСТЬ \d+ · СТРАНИЦА \d+ =====)', full_text)
chunks = []
current = ''
target = 16000
for block in page_blocks:
    if not block.strip():
        continue
    if current and len(current) + len(block) > target:
        chunks.append(current.strip())
        current = block
    else:
        current += block
if current.strip():
    chunks.append(current.strip())

chunk_manifest = []
for i, chunk in enumerate(chunks, start=1):
    path = CHUNK_DIR / f'fragment_{i:03d}.txt'
    path.write_text(chunk, encoding='utf-8')
    page_refs = re.findall(r'===== ЧАСТЬ (\d+) · СТРАНИЦА (\d+) =====', chunk)
    start_ref = page_refs[0] if page_refs else None
    end_ref = page_refs[-1] if page_refs else None
    chunk_manifest.append({
        'fragment': i,
        'path': str(path),
        'chars': len(chunk),
        'start': {'part': int(start_ref[0]), 'page': int(start_ref[1])} if start_ref else None,
        'end': {'part': int(end_ref[0]), 'page': int(end_ref[1])} if end_ref else None,
    })

(BASE / 'manifest.json').write_text(json.dumps({
    'title': 'Дамский салон, части 1–3',
    'parts': manifest,
    'full_text_path': str(full_path),
    'daily_fragments_count': len(chunks),
    'daily_fragments': chunk_manifest,
}, ensure_ascii=False, indent=2), encoding='utf-8')

state_path = BASE / 'state.json'
if not state_path.exists():
    state_path.write_text(json.dumps({'next_fragment': 1, 'completed_fragments': []}, ensure_ascii=False, indent=2), encoding='utf-8')

# Starter instruments file
inst = BASE / 'TOOLS_AND_INSIGHTS.md'
if not inst.exists():
    inst.write_text('# Дамский салон — инструменты, техники, практики, вопросы и осознания\n\nЭтот файл пополняется ежедневными выжимками.\n\n', encoding='utf-8')

print(json.dumps({
    'ok': True,
    'base': str(BASE),
    'parts': manifest,
    'full_text_chars': len(full_text),
    'daily_fragments_count': len(chunks),
    'tools_file': str(inst),
    'state_file': str(state_path),
}, ensure_ascii=False, indent=2))
