#!/usr/bin/env python3
import json
from datetime import date
from pathlib import Path
import subprocess

WORKSPACE = Path("/Users/tonyclaw/.openclaw/workspace")
ETSY_DIR = WORKSPACE / "Etsy"
STATE_PATH = ETSY_DIR / "pipeline-state.json"
PIPELINE_TOOL = ETSY_DIR / "tools" / "pipeline_state.py"

OPPS_DIR = ETSY_DIR / "opportunities"
TODAY = date.today().isoformat()
TODAY_OPPS = OPPS_DIR / f"{TODAY}.json"

MAX_OPPS_PER_DAY = 5
MIN_PRIORITY_TO_WRITE = 3

def run(cmd, check=True):
    return subprocess.run(cmd, check=check, text=True, capture_output=True)

def load_state():
    with open(STATE_PATH, "r", encoding="utf-8") as f:
        return json.load(f)

def existing_ids(state):
    return {it["id"] for it in state.get("items", [])}

def add_to_state(opp):
    cmd = [
        "python3", str(PIPELINE_TOOL),
        "add",
        "--path", str(STATE_PATH),
        "--id", opp["id"],
        "--niche", opp["niche"],
        "--theme", opp["theme"],
        "--priority", str(int(opp["priority"])),
        "--deadline", opp.get("deadline", TODAY),
        "--urgency", str(int(opp.get("urgency", 0))),
    ]
    keywords = opp.get("keywords", []) or []
    avoid = opp.get("avoidList", []) or []
    if keywords:
        cmd += ["--keywords", *keywords]
    if avoid:
        cmd += ["--avoid", *avoid]

    r = run(cmd, check=False)
    if r.returncode != 0:
        raise RuntimeError(r.stderr.strip() or "pipeline_state add failed")

def main():
    OPPS_DIR.mkdir(parents=True, exist_ok=True)

    # ✅ If Scout hasn't generated the opp file yet, exit cleanly.
    if not TODAY_OPPS.exists():
        print(f"Scout runner: no opportunities file yet ({TODAY_OPPS}). Nothing to ingest.")
        return

    with open(TODAY_OPPS, "r", encoding="utf-8") as f:
        data = json.load(f)

    opps = data.get("opportunities", [])
    if not isinstance(opps, list):
        raise SystemExit("Invalid opportunities format: opportunities must be a list")

    state = load_state()
    seen = existing_ids(state)
    MAX_PIPELINE_PER_DAY = 3

    # Count items already created today (so ingest is capped even across multiple runs)
    today_prefix = date.today().isoformat()  # "YYYY-MM-DD"
    existing_today = [
        item for item in state.get("items", [])
        if str(item.get("created_at", "")).startswith(today_prefix)
    ]
    
    added = 0
    for opp in opps:
        if len(existing_today) >= MAX_PIPELINE_PER_DAY:
            break
        if not isinstance(opp, dict):
            continue
        if int(opp.get("priority", 0)) < MIN_PRIORITY_TO_WRITE:
            continue

        oid = opp.get("id")
        if not oid or oid in seen:
            continue

        # Required sanity
        if not opp.get("theme") or not opp.get("niche"):
            continue

        add_to_state(opp)
        added += 1
        existing_today.append({"created_at": today_prefix})
        seen.add(oid)

    print(f"Scout ingest complete. Added {added} opportunities to pipeline-state.")

if __name__ == "__main__":
    main()
