"""Make CSV copies of the Stata data, plus a codebook, for people without Stata.

Run from the package root:
    python3 replication/make_csv.py data/movie.dta data/mktbox.txt data/

Writes data/movie.csv, data/codebook.csv and data/mktbox.csv.
Dates in the original files are SAS dates (days since 1 Jan 1960); the CSVs keep
them and add ISO-format copies.
"""
import sys
from pathlib import Path
import pandas as pd

dta, mkt, out = Path(sys.argv[1]), Path(sys.argv[2]), Path(sys.argv[3])
SAS0 = pd.Timestamp("1960-01-01")

with pd.io.stata.StataReader(dta) as rdr:
    labels = rdr.variable_labels()
d = pd.read_stata(dta, convert_categoricals=False, convert_dates=False)

csv = d.copy()
for c in ("opendate", "close", "revdate"):
    if c in csv:
        csv[c + "_iso"] = (SAS0 + pd.to_timedelta(csv[c], unit="D")).dt.strftime("%Y-%m-%d")
csv.to_csv(out / "movie.csv", index=False)

# Notes added in 2026 for variables the original file left unlabelled (from the
# paper's text and footnote 11, and from how the authors' do-files build them).
PROD = "Distributor group dummy (paper fn. 11): disney = Disney incl. Buena Vista, Miramax; " \
       "sony = Sony, Columbia, Sony Classics, TriStar; fox = Fox, Fox Searchlight; " \
       "mgm_ua = MGM/UA, MGM, Goldwyn, United Artists; gram = Gramercy; orion = Orion; " \
       "univers = Universal; warner = Warner Bros., New Line, Fine Line; paramnt = Paramount"
TIMING = {"pre": "before", "dur": "during", "aft": "after"}
NOTES = {"title": "Movie title (with year)", "major": "Dummy for major distributor; definition not documented (all 612 with major=1 are in one "
                  "of the nine groups, but 13 group members have major=0). Not used in the paper's tables",
         "month": "Month of opening (1-12)",
         "winter": "Season dummy", "spring": "Season dummy", "summer": "Season dummy", "fall": "Season dummy"}
for p in PROD.split(": ", 1)[1].split("; "):
    NOTES[p.split(" = ")[0]] = PROD
for rev, desc in (("uu", "two thumbs up"), ("dd", "two thumbs down"), ("ou", "exactly one thumb up"),
                  ("s", "Siskel thumb up"), ("e", "Ebert thumb up")):
    for t, when in TIMING.items():
        NOTES[rev + t] = f"Interaction: {desc} x review {when} opening weekend"

rows = []
for c in d.columns:
    s = d[c]
    num = pd.api.types.is_numeric_dtype(s)
    rows.append(dict(
        variable=c,
        stata_label=labels.get(c, ""),
        note_2026=NOTES.get(c, "") if not labels.get(c) else "",
        type=str(s.dtype),
        n_nonmissing=int(s.notna().sum()),
        mean=round(float(s.mean()), 4) if num else "",
        min=round(float(s.min()), 4) if num else "",
        max=round(float(s.max()), 4) if num else "",
    ))
pd.DataFrame(rows).to_csv(out / "codebook.csv", index=False)

m = pd.read_csv(mkt, sep=r"\s+", header=None, names=["sas_date", "mktbox_nominal_usd"])
m.insert(1, "date_iso", (SAS0 + pd.to_timedelta(m.sas_date, unit="D")).dt.strftime("%Y-%m-%d"))
m.to_csv(out / "mktbox.csv", index=False)

print(f"movie.csv: {csv.shape}, codebook.csv: {len(rows)} variables, mktbox.csv: {m.shape}")
