/*
 * py_filegen_batch.pgc - py 지급파일 생성 배치 (커서 -> 고정길이 파일 + XA).
 *
 * ATMI client: opens the ECPG XA RM, starts ONE global transaction, inserts a
 * py_file header, then cursors승인(A)된 지급건 for a biz_date writing one
 * fixed-length record per payout into PAYOUT_<bizdate>.dat and stamping each
 * payout with the file_id (status 'F') via the dbio layer, then tpcommit drives
 * XA 2PC. Usage: py_filegen_batch [YYYY-MM-DD]
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atmi.h>
#include "py_dbio.h"

int main(int argc, char **argv)
{
    const char *bizdate = (argc > 1) ? argv[1] : "2026-07-19";
    char path[128], rec[128];
    FILE *fp;
    long rows = 0;

    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16], h_fname[64], h_acct[32];
    long h_file, h_pay, h_net, h_cnt, h_sum;
    EXEC SQL END DECLARE SECTION;

    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    snprintf(h_fname, sizeof(h_fname), "PAYOUT_%s.dat", bizdate);
    snprintf(path, sizeof(path), "/tmp/%s", h_fname);

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

    EXEC SQL SELECT count(*), coalesce(sum(net),0) INTO :h_cnt, :h_sum
        FROM py_payout WHERE biz_date = :h_bizdate AND status = 'A';
    if (sqlca.sqlcode < 0) { fprintf(stderr, "COUNT FAIL [%d] %s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); tpabort(0); return 1; }

    EXEC SQL SELECT nextval('py_file_seq') INTO :h_file;
    if (sqlca.sqlcode < 0) { tpabort(0); return 1; }
    EXEC SQL INSERT INTO py_file (file_id, biz_date, file_name, record_count, total_amount, status)
        VALUES (:h_file, :h_bizdate, :h_fname, :h_cnt, :h_sum, 'G');
    if (sqlca.sqlcode < 0) { fprintf(stderr, "FILE INS FAIL [%d] %s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); tpabort(0); return 1; }

    fp = fopen(path, "w");
    if (fp == NULL) { fprintf(stderr, "fopen FAIL: %s\n", path); tpabort(0); return 1; }

    EXEC SQL DECLARE fgc CURSOR FOR
        SELECT payout_id, account_no, net FROM py_payout
        WHERE biz_date = :h_bizdate AND status = 'A'
        ORDER BY payout_id;
    EXEC SQL OPEN fgc;
    if (sqlca.sqlcode < 0) { fprintf(stderr, "OPEN FAIL [%d] %s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); fclose(fp); tpabort(0); return 1; }

    for (;;) {
        EXEC SQL FETCH fgc INTO :h_pay, :h_acct, :h_net;
        if (sqlca.sqlcode == 100) break;
        if (sqlca.sqlcode < 0) { fprintf(stderr, "FETCH FAIL [%d] %s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); EXEC SQL CLOSE fgc; fclose(fp); tpabort(0); return 1; }
        /* 고정길이 레코드: payout_id(12) account_no(20) net(15) = 47 bytes + NL */
        snprintf(rec, sizeof(rec), "%012ld%-20.20s%015ld\n", h_pay, h_acct, h_net);
        fputs(rec, fp);
        if (pydb_set_file(h_pay, h_file, "F") < 0) {
            EXEC SQL CLOSE fgc; fclose(fp); tpabort(0); return 1;
        }
        rows++;
    }
    EXEC SQL CLOSE fgc;
    fclose(fp);

    if (tpcommit(0) < 0) { fprintf(stderr, "tpcommit FAIL: %s\n", tpstrerror(tperrno)); tpabort(0); return 1; }
    printf(">>> py_filegen_batch COMMIT: file=%s file_id=%ld records=%ld total=%ld\n",
           path, h_file, rows, h_sum);

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