/*
 * py_account_batch.pgc - py 계좌 현황 집계 (dbio 전용 배치, XA).
 *
 * Uses only the dbio layer (no direct EXEC SQL here) to read the active-account
 * balance total and count under ONE XA transaction. Usage: py_account_batch
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atmi.h>
#include "py_dbio.h"
#include "py_account_agg_dbio.h"

int main(int argc, char **argv)
{
    long total, cnt;

    (void)argc; (void)argv;

    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; }

    total = pydb_account_active_balance();
    if (total < 0) { fprintf(stderr, "py_account_batch balance FAIL\n"); tpabort(0); return 1; }
    cnt = pydb_account_active_count();
    if (cnt < 0) { fprintf(stderr, "py_account_batch count FAIL\n"); tpabort(0); return 1; }

    if (tpcommit(0) < 0) { fprintf(stderr, "tpcommit FAIL: %s\n", tpstrerror(tperrno)); tpabort(0); return 1; }
    printf(">>> py_account_batch COMMIT: active_accounts=%ld total_balance=%ld\n", cnt, total);

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