import os
import json
import re
from pathlib import Path
import tkinter as tk
from tkinter import filedialog

def extract_sophora_text(data):
    results = []
    if isinstance(data, list):
        for item in data:
            paragraphs = item.get("sophora-extension:paragraph", [])
            for para in paragraphs:
                text = para.get("sophora-extension:text")
                if text:
                    results.append(clean_html(text))
    return results

# Keys to retain in the filtered JSON
REQUIRED_KEYS = [
    "sophora:tags",
    "wdr:leadin",
    "wdr:schlagzeile",
    "wdr:teasertext",
]

# Clean HTML tags (basic)
def clean_html(value):
    if not isinstance(value, str):
        return value
    # Remove specific tags
    value = re.sub(r'<[^>]+>', '', value)
    return value.strip()


# Merge and filter two JSON objects
def merge_and_filter(json1, json2, filename1, filename2):
    merged = {"source_files": [os.path.basename(filename1), os.path.basename(filename2)]}

    value1_fields = {}
    value2_fields = {}

    for key in REQUIRED_KEYS:
        value1 = clean_html(json1.get(key))
        value2 = clean_html(json2.get(key))
        value1_fields[key] = value1
        value2_fields[key] = value2

    sophora_text_1 = clean_html(extract_sophora_text(json1.get("wdr:text", [])))
    sophora_text_2 = clean_html(extract_sophora_text(json2.get("wdr:text", [])))
    value1_fields["sophora-extension:text"] = sophora_text_1
    value2_fields["sophora-extension:text"] = sophora_text_2

    merged["file_contents"] = [
        dict(value1_fields), dict(value2_fields)
    ]

    return merged

# Read all JSON files in the directory
def load_json_files(folder_path):
    files = sorted([f for f in os.listdir(folder_path) if f.endswith('.json')])
    return [os.path.join(folder_path, f) for f in files]


def select_input_folder():
    root = tk.Tk()
    root.withdraw()  # Hide main window
    folder_selected = filedialog.askdirectory(title="Select Folder Containing JSON Files")
    return folder_selected


def main():
    input_folder = select_input_folder()
    if not input_folder:
        print("No folder selected. Exiting.")
        return

    parent_folder = os.path.dirname(input_folder)
    output_folder = os.path.join(parent_folder, "filtered_output")

    Path(output_folder).mkdir(parents=True, exist_ok=True)

    json_paths = load_json_files(input_folder)

    if len(json_paths) < 2:
        print("Not enough JSON files to process.")
        return

    files_buffer = []
    paths_buffer = []
    file: int = 0
    skipped: int = 0
    for i in range(0, len(json_paths), 1):
        try:
            with (open(json_paths[i], 'r', encoding='utf-8') as f1):
                json_i = json.load(f1)
                sophora_text = clean_html(extract_sophora_text(json_i.get("wdr:text", [])))

                if len(sophora_text) < 4:
                    skipped += 1
                    print(f"This file has fewer than 4 texts: {os.path.basename(json_paths[i])}")
                    continue
                else:
                    files_buffer.append(json_i)
                    paths_buffer.append(json_paths[i])

                    if len(files_buffer) == 2:
                        file += 1
                        merged_filtered = merge_and_filter(files_buffer[0], files_buffer[1], paths_buffer[0], paths_buffer[1])
                        output_filename = f'merged_filtered_{file}.json'
                        output_path = os.path.join(output_folder, output_filename)

                        with open(output_path, 'w', encoding='utf-8') as outfile:
                            json.dump(merged_filtered, outfile, ensure_ascii=False, indent=4)

                        files_buffer.clear()
                        paths_buffer.clear()

        except json.JSONDecodeError:
            print(f"Error decoding JSON files: {json_paths[i]}")
            continue

    print(f"\n✅ Processed {len(json_paths)} files into {file} merged JSON files. (Skipped {skipped} files).")
    print(f"📁 Output saved in: {output_folder}")


if __name__ == "__main__":
    main()
