"""Train only from a reviewed historical CSV; never train from the current tick alone.

Expected columns: nifty, gift_nifty, india_vix, usd_inr, next_open, current_close.
The label is next_open > current_close.
"""
import argparse
import pandas as pd
from app.prediction.features import build_features, FEATURE_COLUMNS
from app.prediction.model import train


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("csv", help="Path to chronological daily/15:15 historical input")
    args = parser.parse_args()
    raw = pd.read_csv(args.csv)
    required = {"nifty", "gift_nifty", "india_vix", "usd_inr", "next_open", "current_close"}
    missing = required - set(raw.columns)
    if missing:
        raise ValueError(f"Training CSV missing columns: {sorted(missing)}")
    features = build_features(raw)
    # Align target after rows dropped by feature calculation.
    target = (raw.loc[features.index, "next_open"] > raw.loc[features.index, "current_close"]).astype(int)
    if target.nunique() < 2:
        raise ValueError("Need both gap-up and non-gap-up examples to train.")
    train(features[FEATURE_COLUMNS], target)
    print(f"Trained baseline with {len(features)} rows.")


if __name__ == "__main__":
    main()
