/* Cross-language data-layer parity for the C / C++ binding: replay the shared
 * golden tick stream through the TickAggregator (push/drain) and check the
 * candles against the Rust reference, with and without gap filling. The same
 * source compiles under both a C and a C++ compiler. Fixtures are generated by
 * `cargo run -p wickra-examples --bin gen_golden`; the golden dir is argv[1].
 */
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wickra.h"

#define MAXROWS 256

static const char *GDIR;

static int read_csv(const char *name, double *rows, int ncol) {
    char path[1024];
    snprintf(path, sizeof(path), "%s/%s.csv", GDIR, name);
    FILE *f = fopen(path, "r");
    if (!f) {
        fprintf(stderr, "cannot open %s\n", path);
        exit(2);
    }
    char line[4096];
    int n = 0, first = 1;
    while (fgets(line, sizeof(line), f)) {
        line[strcspn(line, "\r\n")] = '\0';
        if (first) {
            first = 0;
            continue;
        }
        if (line[0] == '\0') {
            continue;
        }
        char *p = line;
        for (int j = 0; j < ncol; j++) {
            rows[n * ncol + j] = strtod(p, &p);
            if (*p == ',') {
                p++;
            }
        }
        n++;
    }
    fclose(f);
    return n;
}

static int check(const char *fixture, bool gap, const double *ticks, int nt) {
    struct TickAggregator *a = wickra_tick_aggregator_new(1000, gap);
    if (!a) {
        printf("FAIL %s: new returned NULL\n", fixture);
        return 1;
    }
    double want[MAXROWS * 6];
    int nw = read_csv(fixture, want, 6);
    double got[MAXROWS * 6];
    int ng = 0;
    for (int i = 0; i < nt; i++) {
        intptr_t n = wickra_tick_aggregator_push(a, ticks[i * 3 + 0], ticks[i * 3 + 1],
                                                 (int64_t)ticks[i * 3 + 2]);
        if (n > 0) {
            struct WickraCandle buf[MAXROWS];
            intptr_t w = wickra_tick_aggregator_drain(a, buf, (uintptr_t)n);
            for (intptr_t j = 0; j < w; j++) {
                got[ng * 6 + 0] = buf[j].open;
                got[ng * 6 + 1] = buf[j].high;
                got[ng * 6 + 2] = buf[j].low;
                got[ng * 6 + 3] = buf[j].close;
                got[ng * 6 + 4] = buf[j].volume;
                got[ng * 6 + 5] = (double)buf[j].timestamp;
                ng++;
            }
        }
    }
    wickra_tick_aggregator_free(a);
    if (ng != nw) {
        printf("FAIL %s: %d candles vs %d\n", fixture, ng, nw);
        return 1;
    }
    int fails = 0;
    for (int i = 0; i < ng; i++) {
        for (int j = 0; j < 6; j++) {
            double w = want[i * 6 + j];
            double tol = 1e-9 * fmax(1.0, fabs(w));
            if (fabs(got[i * 6 + j] - w) > tol) {
                printf("FAIL %s row %d col %d: %g vs %g\n", fixture, i, j, got[i * 6 + j], w);
                fails++;
            }
        }
    }
    return fails;
}

static int check_resample(void) {
    double input[MAXROWS * 5];
    int ni = read_csv("input", input, 5);
    double want[MAXROWS * 6];
    int nw = read_csv("data_resampled", want, 6);
    struct Resampler *r = wickra_resampler_new(5);
    if (!r) {
        printf("FAIL resample: new returned NULL\n");
        return 1;
    }
    double got[MAXROWS * 6];
    int ng = 0;
    struct WickraCandle out;
    for (int i = 0; i < ni; i++) {
        if (wickra_resampler_update(r, input[i * 5 + 0], input[i * 5 + 1], input[i * 5 + 2],
                                    input[i * 5 + 3], input[i * 5 + 4], (int64_t)i, &out)) {
            got[ng * 6 + 0] = out.open;
            got[ng * 6 + 1] = out.high;
            got[ng * 6 + 2] = out.low;
            got[ng * 6 + 3] = out.close;
            got[ng * 6 + 4] = out.volume;
            got[ng * 6 + 5] = (double)out.timestamp;
            ng++;
        }
    }
    if (wickra_resampler_flush(r, &out)) {
        got[ng * 6 + 0] = out.open;
        got[ng * 6 + 1] = out.high;
        got[ng * 6 + 2] = out.low;
        got[ng * 6 + 3] = out.close;
        got[ng * 6 + 4] = out.volume;
        got[ng * 6 + 5] = (double)out.timestamp;
        ng++;
    }
    wickra_resampler_free(r);
    if (ng != nw) {
        printf("FAIL resample: %d candles vs %d\n", ng, nw);
        return 1;
    }
    int fails = 0;
    for (int i = 0; i < ng; i++) {
        for (int j = 0; j < 6; j++) {
            double w = want[i * 6 + j];
            double tol = 1e-9 * fmax(1.0, fabs(w));
            if (fabs(got[i * 6 + j] - w) > tol) {
                printf("FAIL resample row %d col %d: %g vs %g\n", i, j, got[i * 6 + j], w);
                fails++;
            }
        }
    }
    return fails;
}

static int check_reader(void) {
    char path[1024];
    snprintf(path, sizeof(path), "%s/data_csv.csv", GDIR);
    FILE *f = fopen(path, "rb");
    if (!f) {
        printf("FAIL reader: cannot open %s\n", path);
        return 1;
    }
    static char buf[1 << 16];
    size_t len = fread(buf, 1, sizeof(buf), f);
    fclose(f);

    struct CandleReader *r = wickra_candle_reader_new((const uint8_t *)buf, len);
    if (!r) {
        printf("FAIL reader: new returned NULL\n");
        return 1;
    }
    double want[MAXROWS * 6];
    int nw = read_csv("data_csv_candles", want, 6);
    uintptr_t n = wickra_candle_reader_count(r);
    struct WickraCandle cands[MAXROWS];
    uintptr_t got = wickra_candle_reader_read(r, cands, n);
    wickra_candle_reader_free(r);
    if ((int)got != nw) {
        printf("FAIL reader: %d candles vs %d\n", (int)got, nw);
        return 1;
    }
    int fails = 0;
    for (uintptr_t i = 0; i < got; i++) {
        double row[6] = {cands[i].open,   cands[i].high,   cands[i].low,
                         cands[i].close,  cands[i].volume, (double)cands[i].timestamp};
        for (int j = 0; j < 6; j++) {
            double w = want[i * 6 + j];
            double tol = 1e-9 * fmax(1.0, fabs(w));
            if (fabs(row[j] - w) > tol) {
                printf("FAIL reader row %d col %d: %g vs %g\n", (int)i, j, row[j], w);
                fails++;
            }
        }
    }
    return fails;
}

int main(int argc, char **argv) {
    GDIR = (argc > 1) ? argv[1] : "testdata/golden";
    double ticks[MAXROWS * 3];
    int nt = read_csv("data_ticks", ticks, 3);
    int fails = check("data_candles", false, ticks, nt);
    fails += check("data_candles_gap", true, ticks, nt);
    fails += check_resample();
    fails += check_reader();
    if (fails == 0) {
        printf("C/C++ data layer: OK (%d ticks)\n", nt);
    }
    return fails ? 1 : 0;
}
