# retoor <retoor@molodetz.nl>
import argparse
import json
import re

BOOST_DOMAINS = (".edu", ".gov", ".org", "wikipedia", "arxiv", "ieee", "acm", "nature", "sciencedirect")
MIN_WORDS = 50
MIN_OVERALL = 3.0
DATE_PATTERN = re.compile(r"\b(202[4-9]|203[0-9])\b")


def grade(text, question, url="", intent_hits=None):
    text_lower = text.lower()
    question_words = {word for word in re.findall(r"\w+", question.lower()) if len(word) > 2}
    matches = sum(1 for word in question_words if word in text_lower)
    relevance = min(matches / max(len(question_words), 1) * 10, 10)
    words = len(text.split())
    depth = min(words / 200, 10)
    authority = 5.0
    haystack = url.lower() if url else text_lower[:500]
    for signal in BOOST_DOMAINS:
        if signal in haystack:
            authority = min(authority + 2, 10)
            break
    freshness = 8.0 if DATE_PATTERN.search(text) else 5.0
    if intent_hits is not None:
        relevance = min(relevance + intent_hits * 0.8, 10)
        if intent_hits == 0:
            relevance = max(relevance - 1.5, 0)
    overall = relevance * 0.45 + depth * 0.2 + authority * 0.2 + freshness * 0.15
    return {
        "overall": round(overall, 1),
        "relevance": round(relevance, 1),
        "depth": round(depth, 1),
        "authority": round(authority, 1),
        "freshness": round(freshness, 1),
        "word_count": words,
    }


def keep(result):
    return result["word_count"] >= MIN_WORDS and result["overall"] >= MIN_OVERALL


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Grade a source text 0-10 and decide KEEP or DROP.")
    parser.add_argument("query", help="Research question the source must answer")
    parser.add_argument("textfile", help="File holding the extracted source text")
    parser.add_argument("--url", default="", help="Source URL, used for authority boost matching")
    parser.add_argument("--intent-hits", type=int, default=None, help="Detected intent matches in the text")
    args = parser.parse_args()
    with open(args.textfile, encoding="utf-8") as handle:
        scored = grade(handle.read(), args.query, url=args.url, intent_hits=args.intent_hits)
    scored["verdict"] = "KEEP" if keep(scored) else "DROP"
    print(json.dumps(scored))
