from __future__ import annotations

from online_mining_common import (
    compact_text,
    emit_error,
    emit_success,
    extract_case,
    fetch_one_by_request_id,
    load_json_payload,
    string_values,
    write_optional_jsonl,
)


def main() -> int:
    try:
        payload = load_json_payload()
        request_ids = string_values(payload.get("request_ids"))
        if not request_ids:
            raise ValueError("request_ids is required")
        sources = payload.get("sources") or ["main", "pre_processing"]
        date = payload.get("date")
        date_from = payload.get("date_from")
        date_to = payload.get("date_to")
        lookback_days = payload.get("lookback_days")
        lookback_days = int(lookback_days) if lookback_days not in (None, "") else None
        rows = []
        for request_id in request_ids:
            item = {"request_id": request_id}
            for source in sources:
                doc = fetch_one_by_request_id(
                    str(source),
                    request_id,
                    date,
                    date_from=date_from,
                    date_to=date_to,
                    lookback_days=lookback_days,
                )
                item[str(source)] = extract_case(str(source), doc) if doc else None
            rows.append(item)
        output_path = write_optional_jsonl(str(payload.get("output_path") or ""), rows)
        emit_success(
            {
                "date": date or (f"{date_from}..{date_to}" if date_from and date_to else f"past-{lookback_days}d" if lookback_days else "past-48h"),
                "count": len(rows),
                "output_path": output_path,
                "cases": [
                    {
                        source: (
                            {
                                key: compact_text(value, 1000)
                                for key, value in (case or {}).items()
                                if key != "raw" and value not in (None, "", [], {})
                            }
                            if isinstance(case, dict)
                            else case
                        )
                        for source, case in row.items()
                    }
                    for row in rows
                ],
            }
        )
        return 0
    except Exception as exc:  # noqa: BLE001
        emit_error(exc)
        return 1


if __name__ == "__main__":
    raise SystemExit(main())
