
from typing import Tuple, List, Any
import json
import logging
from wdr_article_semantic_chunking_2.text_util.sw_writer import write_to_file, write_to_file_with_index, write_split
def read_json(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return json.load(f)
    except (FileNotFoundError, json.JSONDecodeError) as e:
        logging.error(f"Error reading {file_path}: {e}")
        return {}

# it also marks sentences, segregate text and writes to file for debug
def extract_texts_and_write_to_file(file_name, debug_mode :bool = True)-> tuple[list[Any], list[Any], Any, dict[str,int]]:
    data = read_json(f"/home/nurseiit/PycharmProjects/wdr-article-semantic-chunking-2/data/{file_name}")

    texts = []
    boundaries = []
    current_index = 0
    debug_index = 0
    dict_for_debug = {}
    file_name = f"{file_name}.txt"

    clear_file_content(file_name, debug_mode=debug_mode)

    for index, content in enumerate(data.get("file_contents", [])):
        article_texts = content.get("sophora-extension:text", [])
        for i in article_texts:
            dict_for_debug[debug_index]=i
            write_to_file_with_index(i, line_number=debug_index, output_filename = file_name, should_write=debug_mode)
            debug_index= debug_index + 1
        write_split(output_filename=file_name,should_write=debug_mode)
        texts.extend(article_texts)
        current_index += len(article_texts)
        boundaries.append(current_index - 1)
    source_count = data.get("source_files", 0)
    return texts, boundaries,len(source_count), dict_for_debug


def clear_file_content(file_name: str, debug_mode:bool):
    if debug_mode:
        from pathlib import Path
        folder_name = "content"
        path = Path.cwd() / folder_name / file_name
        path.parent.mkdir(parents=True, exist_ok=True)
        # Open in write mode to clear the file
        with open(path, "w") as f:
            f.write("")  # optional, just opening in 'w' clears it

# file_name = f"merged_filtered_{3}.json"
# original_data, expected_boundary, source_count, er = extract_texts_and_write_to_file(file_name)
