"""Self-lensing pulse search in TESS SPOC 2-minute light curves: shared preprocessing, the
lensing-template matched filter both methods use, and the features the flare and artifact
controls read. Everything here is deterministic given its inputs."""

import numpy as np
import pandas as pd
from astropy.io import fits
from scipy.signal import oaconvolve

CADENCE_D = 2.0 / 1440.0          # 2-minute cadence, in days
GAP_D = 0.2                       # split the light curve into chunks at gaps longer than this
TREND_WINDOW = 1441               # running-median window: 2 days of 2-minute cadences
DURATIONS_H = np.array([2.0, 3.0, 4.5, 7.0, 10.5, 16.0, 24.0])   # template durations searched
U1, U2 = 0.40, 0.20               # quadratic limb darkening, TESS band, near-solar star
S_MIN = 3.0                       # events are kept down to this S/N; thresholds sit above it
MIN_COVERAGE = 0.5                # a template needs at least half its weight on good cadences
DESAT_BIT = 32                    # SPOC quality bit for reaction-wheel desaturation (momentum dump)
FLARE_HALF_WIDTHS_MIN = np.array([2, 4, 8, 15, 30, 60, 120, 240], float)


def pulse_shape(x, b=0.0, u1=U1, u2=U2):
    """Self-lensing pulse of a point lens much smaller than its source: the excess flux follows
    the limb-darkened surface brightness under the lens. x runs from -1 to 1 across the chord;
    the shape is 1 at the chord's middle and 0 outside it."""
    x = np.asarray(x, float)
    inside = np.abs(x) < 1
    r2 = b * b + (x * x) * (1 - b * b)
    mu = np.sqrt(np.clip(1 - r2, 0, 1))
    intensity = 1 - u1 * (1 - mu) - u2 * (1 - mu) ** 2
    mu0 = np.sqrt(1 - b * b)
    peak = 1 - u1 * (1 - mu0) - u2 * (1 - mu0) ** 2
    return np.where(inside, intensity / peak, 0.0)


def flare_shape(x):
    """Davenport et al. (2014) empirical flare template; x is time from peak over the half width."""
    x = np.asarray(x, float)
    rise = 1 + 1.941 * x - 0.175 * x ** 2 - 2.246 * x ** 3 - 1.125 * x ** 4
    decay = 0.6890 * np.exp(-1.600 * x) + 0.3030 * np.exp(-0.2783 * x)
    return np.where((x >= -1) & (x <= 0), np.clip(rise, 0, None), np.where(x > 0, decay, 0.0))


def lens_kernel(duration_h):
    n = int(round(duration_h * 30))            # cadences across the chord
    k = np.arange(-(n // 2), n // 2 + 1)
    return pulse_shape(2.0 * k / n)


def flare_kernel(half_width_min):
    # Centred on the flare's peak (zero before the rise), so filtering puts the peak at the
    # output cadence.
    h = half_width_min / 2.0                     # half width in cadences
    tail = int(np.ceil(h * 12))                  # by 12 half widths the decay has fallen to ~1% of the peak
    k = np.arange(-tail, tail + 1)
    return flare_shape(k / h)


def load(path):
    """A SPOC light curve on its cadence grid, flagged cadences included."""
    with fits.open(path, memmap=False) as h:
        d = h[1].data
        hdr0, hdr1, ap = h[0].header, h[1].header, h[2].header
        cad = np.asarray(d["CADENCENO"], np.int64)
        t = np.asarray(d["TIME"], float)
        ok = np.isfinite(t)
        coef = np.polyfit(cad[ok], t[ok], 1)
        t = np.where(ok, t, np.polyval(coef, cad))   # times of cadences SPOC left blank
        lc = {
            "tic": int(hdr0["TICID"]), "sector": int(hdr0["SECTOR"]),
            "camera": int(hdr0["CAMERA"]), "ccd": int(hdr0["CCD"]),
            "tmag": float(hdr0["TESSMAG"]),
            "crowdsap": float(hdr1.get("CROWDSAP", 1.0) or 1.0),
            "time": t, "time_ok": ok,
            "flux": np.asarray(d["PDCSAP_FLUX"], float),
            "quality": np.asarray(d["QUALITY"], np.int64),
            "c1": np.asarray(d["MOM_CENTR1"], float), "c2": np.asarray(d["MOM_CENTR2"], float),
            "bkg": np.asarray(d["SAP_BKG"], float),
            "target_col": float(ap["CRVAL1P"] + ap["CRPIX1"] - 1),
            "target_row": float(ap["CRVAL2P"] + ap["CRPIX2"] - 1),
        }
    if np.any(np.diff(cad) != 1):
        raise ValueError(f"TIC {lc['tic']}: cadences are not consecutive")
    return lc


def good_mask(lc):
    return (lc["quality"] == 0) & np.isfinite(lc["flux"]) & lc["time_ok"]


def chunks_of(lc, good):
    """Chunk number of each good cadence (-1 elsewhere): a new chunk after each gap over GAP_D."""
    idx = np.flatnonzero(good)
    chunk = np.full(len(good), -1)
    if len(idx) == 0:
        return chunk, []
    t = lc["time"][idx]
    breaks = np.flatnonzero(np.diff(t) > GAP_D)
    starts = np.concatenate([[0], breaks + 1])
    ends = np.concatenate([breaks + 1, [len(idx)]])
    spans = []
    for c, (a, b) in enumerate(zip(starts, ends)):
        chunk[idx[a]:idx[b - 1] + 1] = c
        spans.append((idx[a], idx[b - 1]))
    chunk[~good] = -1
    return chunk, spans


def running_median(y, good, spans):
    out = np.full(len(y), np.nan)
    for a, b in spans:
        seg = np.where(good[a:b + 1], y[a:b + 1], np.nan)
        out[a:b + 1] = pd.Series(seg).rolling(TREND_WINDOW, center=True, min_periods=30).median().to_numpy()
    return out


def robust_sigma(x):
    x = x[np.isfinite(x)]
    if len(x) == 0:
        return np.nan
    return 1.4826 * np.median(np.abs(x - np.median(x)))


def prepare(lc, injection=None):
    """Normalize, inject (before anything else), mask, chunk, and detrend. injection is
    (model, centroid_shift_col, centroid_shift_row): the fractional pulse on every cadence and
    the photocentre shift it brings, in pixels."""
    good = good_mask(lc)
    flux = lc["flux"] / np.nanmedian(lc["flux"][good])
    c1, c2 = lc["c1"].copy(), lc["c2"].copy()
    if injection is not None:
        model, d1, d2 = injection
        flux = flux * (1 + model)
        c1 = c1 + d1
        c2 = c2 + d2
    chunk, spans = chunks_of(lc, good)
    trend = running_median(flux, good, spans)
    resid = np.where(good, flux / trend - 1, 0.0)
    aux = {}
    for name, y in (("c1", c1), ("c2", c2), ("bkg", lc["bkg"])):
        g = good & np.isfinite(y)
        tr = running_median(y, g, spans)
        aux[name] = (np.where(g, y - tr, 0.0), g)
    sig = np.full(len(spans), np.nan)
    for c, (a, b) in enumerate(spans):
        sig[c] = robust_sigma(resid[a:b + 1][good[a:b + 1]])
    return {"good": good, "chunk": chunk, "spans": spans, "resid": resid, "sigma": sig, "aux": aux}


def _conv(x, k):
    """Correlate x with an odd-length kernel centred on its middle element (output as long as x)."""
    return oaconvolve(x, k[::-1], mode="same")


def filter_snr(y, mask, spans, kernel, sigma=None):
    """Weighted matched filter of y with kernel inside each chunk: the S/N of the best-fitting
    amplitude, in units of the per-chunk white noise when sigma is given (one per chunk), and the
    amplitude itself. Cadences where the kernel has under MIN_COVERAGE of its weight are NaN."""
    snr = np.full(len(y), np.nan)
    amp = np.full(len(y), np.nan)
    w2 = kernel ** 2
    full = w2.sum()
    for c, (a, b) in enumerate(spans):
        m = mask[a:b + 1].astype(float)
        yy = np.where(mask[a:b + 1], y[a:b + 1], 0.0)
        num = _conv(yy * m, kernel)
        den = _conv(m, w2)
        ok = den >= MIN_COVERAGE * full
        s = 1.0 if sigma is None else sigma[c]
        with np.errstate(invalid="ignore", divide="ignore"):
            snr[a:b + 1] = np.where(ok, num / (s * np.sqrt(den)), np.nan)
            amp[a:b + 1] = np.where(ok, num / den, np.nan)
    return snr, amp


def renormalize(snr):
    """Divide by the robust spread of the S/N series itself, so correlated noise counts."""
    scale = robust_sigma(snr)
    return snr / scale if np.isfinite(scale) and scale > 0 else snr


KERNELS = [lens_kernel(d) for d in DURATIONS_H]
FLARE_KERNELS = [flare_kernel(w) for w in FLARE_HALF_WIDTHS_MIN]


def search(prep):
    """The lensing-template matched filter: per-duration S/N, renormalized for correlated noise."""
    snrs, amps, white = [], [], []
    for k in KERNELS:
        s, a = filter_snr(prep["resid"], prep["good"], prep["spans"], k, prep["sigma"])
        white.append(s)
        snrs.append(renormalize(s))
        amps.append(a)
    return np.array(snrs), np.array(amps), np.array(white)


def find_events(lc, snr, amps, s_min=S_MIN):
    """Peaks of the S/N over every duration, strongest first, each masking one full duration on
    either side of it."""
    with np.errstate(invalid="ignore"):
        best = np.nanmax(np.where(np.isfinite(snr), snr, -np.inf), axis=0)
    kbest = np.argmax(np.where(np.isfinite(snr), snr, -np.inf), axis=0)
    work = np.where(np.isfinite(best), best, -np.inf)
    events = []
    order = np.argsort(-work)
    taken = np.zeros(len(work), bool)
    for i in order:
        if work[i] < s_min:
            break
        if taken[i]:
            continue
        k = kbest[i]
        half = int(round(DURATIONS_H[k] * 30))
        taken[max(0, i - half):i + half + 1] = True
        events.append({"i": int(i), "time": float(lc["time"][i]), "snr": float(work[i]),
                       "k": int(k), "duration_h": float(DURATIONS_H[k]), "amp": float(amps[k, i])})
    return events


def _best_in(series, lo, hi):
    """Largest positive value of any row of series in columns lo..hi."""
    seg = series[:, lo:hi + 1]
    seg = seg[np.isfinite(seg)]
    return max(0.0, float(seg.max())) if len(seg) else 0.0


FLARE_REACH = max(len(k) // 2 for k in FLARE_KERNELS)


def flare_snr_near(prep, i, lo, hi):
    """White-noise matched-filter S/N of every flare template, over a stretch of the event's chunk
    reaching FLARE_REACH cadences beyond lo..hi, so values in lo..hi are as over the whole chunk."""
    c = prep["chunk"][i]
    if c < 0:   # the peak sits on a masked cadence; take the nearest chunk
        near = np.flatnonzero(prep["chunk"] >= 0)
        c = prep["chunk"][near[np.argmin(np.abs(near - i))]]
    a, b = prep["spans"][c]
    w_lo, w_hi = max(a, lo - FLARE_REACH), min(b, hi + FLARE_REACH)
    sub = slice(w_lo, w_hi + 1)
    rows = np.full((len(FLARE_KERNELS), len(prep["resid"])), np.nan)
    for j, fk in enumerate(FLARE_KERNELS):
        rows[j, sub] = filter_snr(prep["resid"][sub], prep["good"][sub], [(0, w_hi - w_lo)], fk,
                                  np.array([prep["sigma"][c]]))[0]
    return rows


def event_features(lc, prep, ev, dumps, aux_snr, lens_white):
    """What the controls read about one event."""
    i, k = ev["i"], ev["k"]
    t0 = ev["time"]
    # Flare test: centred within half a duration of the peak, does a flare template fit better
    # than a self-lensing one? With amplitude, centre and width free in both, the difference in
    # chi-square is the difference of their squared white-noise matched-filter S/N.
    half = int(round(ev["duration_h"] * 15))
    lo, hi = max(0, i - half), min(len(prep["resid"]) - 1, i + half)
    lens = _best_in(lens_white, lo, hi)
    flare = _best_in(flare_snr_near(prep, i, lo, hi), lo, hi)
    # Hours from the event's peak to the nearest momentum dump and to the nearest chunk edge.
    dist_dump = float(np.abs(dumps - t0).min() * 24) if len(dumps) else np.inf
    edges = np.array([lc["time"][s] for span in prep["spans"] for s in span])
    dist_edge = float(np.abs(edges - t0).min() * 24)
    cen = max(abs(aux_snr["c1"][k, i]), abs(aux_snr["c2"][k, i]))
    bkg = abs(aux_snr["bkg"][k, i])
    return {"flare_dchi2": flare ** 2 - lens ** 2, "dist_dump_h": dist_dump, "dist_edge_h": dist_edge,
            "centroid_snr": float(np.nan_to_num(cen)), "background_snr": float(np.nan_to_num(bkg))}


def aux_filter(prep):
    """Renormalized matched-filter S/N of the centroid and background series, per duration."""
    out = {}
    for name, (y, g) in prep["aux"].items():
        rows = []
        for kern in KERNELS:
            s, _ = filter_snr(y, g, prep["spans"], kern, None)
            rows.append(renormalize(s))
        out[name] = np.array(rows)
    return out


def dump_times(lc):
    return lc["time"][(lc["quality"] & DESAT_BIT) > 0]


def analyse(lc, injection=None, near=None):
    """Run the search on a light curve, optionally with a pulse injected first. near=(t0, half
    window in days) keeps only events whose peak lies in that window, to save work."""
    prep = prepare(lc, injection)
    snr, amps, white = search(prep)
    events = find_events(lc, snr, amps)
    if near is not None:
        t0, w = near
        events = [e for e in events if abs(e["time"] - t0) <= w]
    if events:
        aux = aux_filter(prep)
        dumps = dump_times(lc)
        for e in events:
            e.update(event_features(lc, prep, e, dumps, aux, white))
    return prep, events


CONTAMINANT_PX = 3.0   # farthest, in pixels, light from other stars in an aperture sits from the target


def injection_for(lc, prep, t0, duration_h, b, snr_target, max_amp=0.04):
    """A self-lensing pulse centred at t0 whose expected white-noise matched-filter S/N, against
    this light curve's own scatter, is snr_target; and the photocentre shift it would bring.

    Brightening the target by s moves the flux-weighted centroid toward the target by about
    s * f * (x_target - centroid), f being CROWDSAP. The offset between the catalogue position and
    the measured centroid also holds the PSF's own asymmetry, which a brightening of the target
    doesn't move, so the offset is capped at what contamination alone can make it,
    (1 - f) * CONTAMINANT_PX."""
    x = 2 * (lc["time"] - t0) / (duration_h / 24.0)
    shape = pulse_shape(x, b)
    good = prep["good"]
    inside = good & (shape > 0)
    c = prep["chunk"][inside]
    sigma = np.nanmedian(prep["sigma"][np.unique(c)]) if len(c) else np.nanmedian(prep["sigma"])
    norm = np.sqrt(np.sum(shape[inside] ** 2))
    amp = min(snr_target * sigma / norm, max_amp) if norm > 0 else 0.0
    model = amp * shape
    f = min(lc["crowdsap"], 1.0)
    off = np.array([lc["target_col"] - np.nanmedian(lc["c1"][good]), lc["target_row"] - np.nanmedian(lc["c2"][good])])
    size = float(np.hypot(*off))
    cap = (1 - f) * CONTAMINANT_PX
    if size > cap:
        off = off * (cap / size)
    return (model, model * f * off[0], model * f * off[1]), amp
