# ======================================
# Filter comments that may contain Desires, Aspirations, or Goals
# Saves only candidate comments to a new CSV
# ======================================

import csv
import os
import sys
import re
import pandas as pd

# Lista de trigger words (regex cu word boundaries)
TRIGGERS = [
    # Desire / need
    r"\bwant\b", r"\bneed\b", r"\bcrave\b", r"\bdesire\b", r"\brequire\b",
    r"\bmust have\b", r"\blonging\b", r"\bwould like\b", r"\blooking for\b",

    # Aspirations / hopes / dreams
    r"\bwish\b", r"\bhope\b", r"\bdream\b", r"\baspire\b", r"\baspiration\b",
    r"\bwould love\b", r"\blike to\b", r"\blooking forward\b",
    r"\bit would be nice\b", r"\bcan'?t wait\b",

    # Goals / plans / intentions
    r"\bgoal\b", r"\baim\b", r"\bplan\b", r"\bintend\b", r"\bobjective\b",
    r"\btarget\b", r"\bcommitment\b", r"\bresolution\b", r"\bpurpose\b", r"\bmission\b",

    # Future-oriented softeners
    r"\bsomeday\b", r"\bone day\b", r"\bin the future\b", r"\beventually\b",
    r"\bsome point\b", r"\bdown the line\b"
]
TRIGGER_REGEX = re.compile("|".join(TRIGGERS), flags=re.IGNORECASE)

def likely_contains_desire(comment: str) -> bool:
    if not isinstance(comment, str):
        return False
    return TRIGGER_REGEX.search(comment) is not None

def filter_comments(input_csv, output_csv, column_name="comment"):
    df = pd.read_csv(input_csv)

    if column_name not in df.columns:
        raise ValueError(f"Input file must contain a '{column_name}' column. Found: {list(df.columns)}")

    # Select doar comentariile cu trigger words
    candidates = df[df[column_name].apply(likely_contains_desire)]

    # Salvează în CSV
    candidates.to_csv(output_csv, index=False)

    print(f"✅ Found {len(candidates)} candidate comments out of {len(df)} total.")
    print(f"Results saved to {output_csv}")

# ---------------------------
# Run from command line
# ---------------------------
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python filter_desires.py <input_csv>")
        sys.exit(1)

    input_csv = sys.argv[1]
    base, ext = os.path.splitext(input_csv)
    output_csv = base + "_candidates.csv"

    filter_comments(input_csv, output_csv)
