"""
edgar_events.py -- [2026-10-07] SEC EDGAR CORPORATE-EVENT RECORDER (the user's idea, phase 1 = RECORD ONLY; nothing trades
on it). Polls the SEC "latest filings" feed (free, no key; the SEC requires a contact in the User-Agent -- the user's
email, approved 10/7), maps each filing to a ticker (sec.gov/files/company_tickers.json), tags it, and appends it to
data/events/<date>.jsonl. Purpose: (1) an AVOID filter -- merger targets (stock pinned to the deal price), offerings /
dilution, reverse splits / spin-offs (break the 14-day reference data, CTVA 10/6), delisting notices, going concern;
(2) a CATALYST flag -- material agreements, contracts, FDA, acquisitions BY the company, activist stakes;
(3) context for the playbook cases. Phase 2 = backtest each tag on history before any rule uses it.
Tags come from the form type, the 8-K item numbers and, for 8-K/6-K/425/SC TO/14D9, key phrases in the filing text.
    python3 edgar_events.py            (cron 6:00 weekdays; stops itself at 20:00)
"""
import html
import json
import re
import time
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo

import requests

BASE = Path(__file__).resolve().parent
OUT = BASE / "data" / "events"
ET = ZoneInfo("America/New_York")
UA = "ScreenerTradeBot frankseliodasilva@gmail.com"
FEED = "https://www.sec.gov/cgi-bin/browse-edgar?action=getcurrent&type=&company=&dateb=&owner=include&start={start}&count=100&output=atom"
S = requests.Session()
S.headers.update({"User-Agent": UA, "Accept-Encoding": "gzip, deflate"})

FORM_TAGS = {
    "424B1": ("offering", "avoid"), "424B2": ("offering", "avoid"), "424B3": ("offering", "avoid"),
    "424B4": ("offering", "avoid"), "424B5": ("offering", "avoid"), "424B7": ("offering", "avoid"),
    "S-1": ("offering", "avoid"), "F-1": ("offering", "avoid"), "S-3": ("shelf registration", "watch"),
    "F-3": ("shelf registration", "watch"), "S-4": ("merger registration", "watch"), "F-4": ("merger registration", "watch"),
    "SC 14D9": ("tender offer target", "avoid"), "SC TO-T": ("tender offer", "avoid"), "SC TO-I": ("issuer tender offer", "watch"),
    "DEFM14A": ("merger vote (target)", "avoid"), "PREM14A": ("merger vote (target)", "avoid"), "425": ("merger communication", "watch"),
    "SC 13D": ("activist / 5%+ stake", "catalyst"), "SC 13D/A": ("activist stake change", "watch"),
    "SC 13G": ("passive 5%+ stake", "info"), "4": ("insider transaction", "info"), "10-12B": ("spin-off", "avoid"),
    "25-NSE": ("delisting", "avoid"), "15-12B": ("deregistration", "avoid"), "NT 10-K": ("late annual report", "avoid"),
    "NT 10-Q": ("late quarterly report", "avoid"),
}
ITEM_TAGS = {
    "1.01": ("material agreement", "catalyst"), "1.02": ("agreement terminated", "watch"),
    "2.01": ("acquisition/disposition completed", "watch"), "2.02": ("earnings", "info"),
    "3.01": ("delisting notice", "avoid"), "3.02": ("unregistered share sale (dilution)", "avoid"),
    "3.03": ("shareholder rights changed", "watch"), "5.01": ("change of control", "watch"),
    "5.02": ("officer/director change", "info"), "5.03": ("charter/bylaws amendment", "watch"),
    "7.01": ("Reg FD disclosure", "info"), "8.01": ("other events", "info"),
}
PHRASES = [
    (r"agreement and plan of merger|merger agreement|definitive (merger )?agreement to (be )?acquire", "merger agreement", "watch"),
    (r"tender offer", "tender offer", "watch"),
    (r"per share in cash|for \$[\d.]+ per share", "cash deal per share", "avoid"),
    (r"underwritten (public )?offering|registered direct offering|public offering of|pricing of .{0,40}offering", "offering", "avoid"),
    (r"private placement|securities purchase agreement", "private placement", "avoid"),
    (r"at-the-market|at the market offering|equity distribution agreement", "ATM offering", "avoid"),
    (r"reverse (stock )?split", "reverse split", "avoid"),
    (r"spin-?off|separation and distribution", "spin-off", "avoid"),
    (r"going concern", "going concern", "avoid"),
    (r"nasdaq.{0,60}(deficiency|delist)|minimum bid price", "listing deficiency", "avoid"),
    (r"fda (approval|approves|clearance)|breakthrough therapy|fast track designation|pdufa", "FDA", "catalyst"),
    (r"awarded .{0,40}contract|contract award|purchase order|supply agreement", "contract", "catalyst"),
    (r"strategic (investment|partnership|collaboration)|collaboration agreement|license agreement", "partnership", "catalyst"),
    (r"share repurchase|buyback", "buyback", "catalyst"),
]
TEXT_FORMS = {"8-K", "6-K", "425", "SC 14D9", "SC TO-T", "8-K/A"}


def market_tickers():
    j = S.get("https://www.sec.gov/files/company_tickers.json", timeout=30).json()
    m = {}
    for r in j.values():
        m.setdefault(int(r["cik_str"]), []).append(r["ticker"])
    return m


def feed(start=0):
    t = S.get(FEED.format(start=start), timeout=30).text
    out = []
    for e in re.findall(r"<entry>(.*?)</entry>", t, re.S):
        title = html.unescape(re.search(r"<title>(.*?)</title>", e, re.S).group(1))
        m = re.match(r"(.+?) - (.+) \((\d{10})\) \((\w+)\)", title)
        if not m:
            continue
        summ = html.unescape(re.search(r"<summary[^>]*>(.*?)</summary>", e, re.S).group(1))
        out.append({"form": m.group(1).strip(), "company": m.group(2).strip(), "cik": int(m.group(3)), "role": m.group(4),
                    "accession": re.search(r"accession-number=([\d-]+)", e).group(1),
                    "updated": re.search(r"<updated>(.*?)</updated>", e).group(1),
                    "link": re.search(r'href="([^"]+)"', e).group(1),
                    "items": re.findall(r"Item (\d+\.\d+)", summ)})
    return out


def filing_text(index_url, limit=400_000):
    """primary document + EX-99 press releases, plain text (first `limit` chars each)"""
    idx = S.get(index_url, timeout=30).text
    docs = re.findall(r'href="(/Archives/edgar/data/[^"]+\.(?:htm|html|txt))"[^<]*</a></td>\s*<td[^>]*>([^<]*)</td>', idx)
    urls = [u for u, typ in docs if typ.strip() in ("8-K", "6-K", "425", "SC 14D9", "SC TO-T", "8-K/A") or typ.strip().startswith("EX-99")][:3]
    txt = ""
    for u in urls:
        time.sleep(0.2)
        r = S.get("https://www.sec.gov" + u, timeout=30)
        txt += " " + re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", r.text[:limit])))
    return txt


def tag(f):
    tags = []
    base = f["form"].replace("/A", "") if f["form"] not in FORM_TAGS else f["form"]
    if f["form"] in FORM_TAGS or base in FORM_TAGS:
        tags.append(FORM_TAGS.get(f["form"]) or FORM_TAGS[base])
    for it in f["items"]:
        if it in ITEM_TAGS:
            tags.append(ITEM_TAGS[it])
    return tags


def main():
    OUT.mkdir(parents=True, exist_ok=True)
    tick = market_tickers()
    seen, day = set(), None
    while True:
        now = datetime.now(ET)
        if now.hour >= 20:
            print("stopped: 20:00", flush=True); return
        d = now.strftime("%Y-%m-%d")
        if d != day:
            day, seen = d, set()
            f_out = OUT / f"{d}.jsonl"
            if f_out.exists():
                seen = {json.loads(l)["accession"] for l in open(f_out)}
        try:
            new = [f for f in feed() if f["accession"] not in seen]
            for f in reversed(new):
                seen.add(f["accession"])
                f["tickers"] = tick.get(f["cik"], [])
                if not f["tickers"]:
                    continue                                  # funds, trusts, private filers
                f["tags"] = tag(f)
                if f["form"] in TEXT_FORMS and (set(f["items"]) & {"1.01", "2.01", "3.02", "3.03", "5.03", "7.01", "8.01"} or f["form"] != "8-K"):
                    try:
                        txt = filing_text(f["link"]).lower()
                        for pat, name, kind in PHRASES:
                            if re.search(pat, txt):
                                f["tags"].append((name, kind))
                    except Exception as e:
                        f["text_error"] = str(e)[:80]
                kinds = {k for _, k in f["tags"]}
                f["verdict"] = "avoid" if "avoid" in kinds else "catalyst" if "catalyst" in kinds else "watch" if "watch" in kinds else "info"
                f["seen_at"] = datetime.now(ET).isoformat(timespec="seconds")
                with open(OUT / f"{day}.jsonl", "a") as fh:
                    fh.write(json.dumps(f) + "\n")
                if f["verdict"] in ("avoid", "catalyst"):
                    print(f"{f['seen_at'][11:]} {f['verdict'].upper():8} {','.join(f['tickers'])[:20]:20} {f['form']:8} "
                          f"{'; '.join(n for n, _ in f['tags'])[:90]}", flush=True)
        except Exception as e:
            print("poll failed:", str(e)[:120], flush=True)
        time.sleep(30)


if __name__ == "__main__":
    main()
