#!/usr/bin/env python3
"""Test manual chunking by sheet from Docling markdown."""

import sys
import subprocess
import tempfile
import re
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from docling.document_converter import DocumentConverter
from openpyxl import load_workbook


def convert_xls(xls_path: str) -> str:
    """Convert .xls to .xlsx."""
    temp_dir = tempfile.mkdtemp()
    result = subprocess.run([
        '/Applications/LibreOffice.app/Contents/MacOS/soffice',
        '--headless',
        '--convert-to', 'xlsx',
        '--outdir', temp_dir,
        xls_path
    ], capture_output=True, text=True, timeout=60)

    xlsx_files = list(Path(temp_dir).glob('*.xlsx'))
    return str(xlsx_files[0]) if xlsx_files else None


def get_sheet_names(xlsx_path: str):
    """Get sheet names from Excel file."""
    wb = load_workbook(xlsx_path, read_only=True)
    names = wb.sheetnames
    wb.close()
    return names


def chunk_markdown_by_sheet(markdown: str, sheet_names: list) -> dict:
    """
    Manually chunk markdown by detecting sheet boundaries.

    Strategy:
    - Docling processes sheets sequentially
    - Each sheet's content appears in order
    - We need to split the markdown by sheet boundaries
    """

    chunks_by_sheet = {}

    # Split markdown into lines for analysis
    lines = markdown.split('\n')

    print(f"Total markdown lines: {len(lines)}")
    print(f"Total markdown chars: {len(markdown)}")
    print(f"\nSheet names to find: {sheet_names}\n")

    # Try to find sheet boundaries by looking for sheet names in content
    # or by detecting section breaks

    current_sheet_idx = 0
    current_chunk = []

    for i, line in enumerate(lines):
        # Check if this line contains a sheet name (indicating new sheet)
        # This is a heuristic - Docling doesn't explicitly mark sheet boundaries

        # For now, let's just collect all content and try to identify patterns
        current_chunk.append(line)

        # Check if we can find sheet names in the content
        for idx, sheet_name in enumerate(sheet_names):
            if sheet_name in line:
                print(f"Found sheet name '{sheet_name}' at line {i}: {line[:100]}")

    # Alternative approach: Use page numbers from Docling's internal structure
    # For now, let's just analyze the markdown structure

    print(f"\n{'='*80}")
    print("MARKDOWN STRUCTURE ANALYSIS:")
    print(f"{'='*80}\n")

    # Show first 50 lines
    print("First 50 lines:")
    for i, line in enumerate(lines[:50], 1):
        if line.strip():
            print(f"{i:3d}: {line[:120]}")

    print(f"\n{'-'*80}\n")

    # Show lines around sheet name occurrences
    for sheet_name in ['ステータス戻し実施可否判定', 'チェック対象判定']:
        print(f"\nSearching for '{sheet_name}':")
        for i, line in enumerate(lines):
            if sheet_name in line:
                start = max(0, i-2)
                end = min(len(lines), i+3)
                print(f"\n  Found at line {i}:")
                for j in range(start, end):
                    marker = ">>> " if j == i else "    "
                    print(f"  {marker}{j:3d}: {lines[j][:120]}")

    return chunks_by_sheet


def main(file_path: str):
    """Test manual chunking approach."""

    print(f"Testing manual chunking on: {file_path}\n")

    # Convert if needed
    if file_path.endswith('.xls'):
        xlsx_path = convert_xls(file_path)
        print(f"Converted to: {xlsx_path}\n")
    else:
        xlsx_path = file_path

    # Get sheet names
    sheet_names = get_sheet_names(xlsx_path)
    print(f"Sheet names ({len(sheet_names)}): {sheet_names}\n")

    # Convert with Docling
    print("Converting with Docling...")
    converter = DocumentConverter()
    result = converter.convert(Path(xlsx_path))

    # Export to markdown
    markdown = result.document.export_to_markdown()
    print(f"Exported to markdown: {len(markdown)} chars\n")

    # Try manual chunking
    print(f"{'='*80}")
    print("ATTEMPTING MANUAL CHUNKING BY SHEET:")
    print(f"{'='*80}\n")

    chunks = chunk_markdown_by_sheet(markdown, sheet_names)

    # Also check if Docling document has page/sheet information
    print(f"\n{'='*80}")
    print("CHECKING DOCLING DOCUMENT STRUCTURE:")
    print(f"{'='*80}\n")

    doc = result.document

    # Try to access document items with page info
    print("Document has these attributes:")
    print([attr for attr in dir(doc) if not attr.startswith('_')])

    # Check if we can iterate items
    if hasattr(doc, 'iterate_items'):
        print("\nIterating document items:")
        for idx, item in enumerate(doc.iterate_items()):
            if idx < 10:  # Show first 10 items
                item_type = type(item).__name__
                text = str(item)[:80] if hasattr(item, '__str__') else ""
                prov_info = ""

                if hasattr(item, 'prov') and item.prov:
                    for prov in item.prov:
                        if hasattr(prov, 'page_no'):
                            prov_info = f" [page_no={prov.page_no}]"
                            break

                print(f"  {idx}: {item_type}{prov_info}: {text}")


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python test_manual_chunking.py <path_to_excel_file>")
        sys.exit(1)

    main(sys.argv[1])
