import json
from pathlib import Path

from src.data_loader import load_reviews
from src.graph import feedback_graph
from src.report import print_batch_report


DATA_FILE = Path("data/customer_reviews.jsonl")
OUTPUT_FILE = Path("outputs/analyzed_reviews.jsonl")


if __name__ == "__main__":
    reviews = load_reviews(DATA_FILE)
    analyzed = []

    OUTPUT_FILE.parent.mkdir(exist_ok=True)

    with OUTPUT_FILE.open("w", encoding="utf-8") as out:
        for index, review in enumerate(reviews, start=1):
            print(f"Analyzing {index:03}/{len(reviews)} - {review['id']}")

            result = feedback_graph.invoke(
                {
                    "customer_id": review["id"],
                    "feedback": review["feedback"],
                },
                # Four LLM worker nodes may run concurrently for this review.
                config={"max_concurrency": 4},
            )

            final = result["final_analysis"]
            analyzed.append(final)
            out.write(final.model_dump_json() + "\n")

    print_batch_report(analyzed)
    print(f"\nFull structured results saved to: {OUTPUT_FILE}")
