"""
Scan bulk submissions.zip for all 8-K/8-K/A filings with Item 1.05.
Outputs accession-meta-8k.json with filing metadata for extraction.

Much faster than per-CIK unzip -p calls — reads ZIP entries in-memory.
"""
import json
import sys
import zipfile
from pathlib import Path

ZIP_PATH = Path("../data/bulk/submissions.zip")
OUTPUT_PATH = Path("../data/bulk/accession-meta-8k.json")
DATE_START = "2023-12-18"

def main():
    if not ZIP_PATH.exists():
        print(f"  ✖ {ZIP_PATH} not found", file=sys.stderr)
        sys.exit(1)

    results: dict[str, dict] = {}
    total_ciks = 0
    total_8k = 0

    with zipfile.ZipFile(ZIP_PATH, "r") as zf:
        # Only process main CIK files (not overflow files)
        cik_files = [n for n in zf.namelist() if n.startswith("CIK") and n.endswith(".json") and "-" not in n]
        print(f"  {len(cik_files)} CIK files in ZIP", file=sys.stderr)

        for i, name in enumerate(cik_files):
            if (i + 1) % 10000 == 0:
                print(f"\r  Scanned {i + 1}/{len(cik_files)} CIKs, found {len(results)} Item 1.05 filings", end="", file=sys.stderr)

            try:
                data = json.loads(zf.read(name))
            except (json.JSONDecodeError, KeyError):
                continue

            total_ciks += 1
            recent = data.get("filings", {}).get("recent", {})
            forms = recent.get("form", [])
            items_list = recent.get("items", [])
            accessions = recent.get("accessionNumber", [])
            dates = recent.get("filingDate", [])
            primary_docs = recent.get("primaryDocument", [])

            cik = str(data.get("cik", ""))
            company_name = data.get("name", "")
            tickers = data.get("tickers", [])
            ticker = tickers[0] if tickers else ""

            for j in range(len(forms)):
                form = forms[j] if j < len(forms) else ""
                if form not in ("8-K", "8-K/A"):
                    continue

                total_8k += 1
                filing_date = dates[j] if j < len(dates) else ""
                if filing_date < DATE_START:
                    continue

                items = items_list[j] if j < len(items_list) else ""
                # Item 1.05 = cybersecurity incident disclosure
                if "1.05" not in items:
                    continue

                accession = accessions[j] if j < len(accessions) else ""
                primary_doc = primary_docs[j] if j < len(primary_docs) else ""

                results[accession] = {
                    "companyName": company_name,
                    "cik": cik,
                    "ticker": ticker,
                    "filingDate": filing_date,
                    "form": form,
                    "items": items,
                    "primaryDocument": primary_doc,
                }

    print(f"\r  Scanned {total_ciks} CIKs, {total_8k} total 8-K filings", file=sys.stderr)
    print(f"  Found {len(results)} 8-K filings with Item 1.05", file=sys.stderr)

    # Also count by form type
    forms_count: dict[str, int] = {}
    for r in results.values():
        f = r["form"]
        forms_count[f] = forms_count.get(f, 0) + 1
    for f, c in sorted(forms_count.items()):
        print(f"    {f}: {c}", file=sys.stderr)

    OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
    with open(OUTPUT_PATH, "w") as f:
        json.dump(results, f, indent=2)
    print(f"  Wrote {OUTPUT_PATH}", file=sys.stderr)


if __name__ == "__main__":
    main()
