/*
 * au_fraud_summary_batch.pgc - au 부정거래 요약 (dbio 헬퍼 호출 배치, XA).
 *
 * Instead of inline SQL, this batch drives the libaudbio.a query helpers
 * (audb_fraud_avg_score + audb_auth_count_by_date) under ONE global XA
 * transaction and prints a summary. Usage: au_fraud_summary_batch [YYYY-MM-DD]
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atmi.h>
#include "au_dbio.h"
#include "au_fraud_agg_dbio.h"
#include "au_auth_agg_dbio.h"

int main(int argc, char **argv)
{
    const char *bizdate = (argc > 1) ? argv[1] : "2026-07-19";
    long avg_score, auth_cnt;

    if (tpinit(NULL) < 0) { fprintf(stderr, "tpinit FAIL: %s\n", tpstrerror(tperrno)); return 1; }
    if (tpopen() < 0)     { fprintf(stderr, "tpopen FAIL: %s\n", tpstrerror(tperrno)); return 1; }
    if (tpbegin(60, 0) < 0) { fprintf(stderr, "tpbegin FAIL: %s\n", tpstrerror(tperrno)); return 1; }

    avg_score = audb_fraud_avg_score(bizdate);
    if (avg_score < 0) { fprintf(stderr, "avg_score FAIL\n"); tpabort(0); return 1; }
    auth_cnt = audb_auth_count_by_date(bizdate);
    if (auth_cnt < 0) { fprintf(stderr, "auth_cnt FAIL\n"); tpabort(0); return 1; }

    if (tpcommit(0) < 0) { fprintf(stderr, "tpcommit FAIL: %s\n", tpstrerror(tperrno)); tpabort(0); return 1; }
    printf(">>> au_fraud_summary_batch COMMIT: bizdate=%s auths=%ld avg_fraud_score=%ld\n", bizdate, auth_cnt, avg_score);

    tpclose(); tpterm();
    return 0;
}
