/*
 * mm_holiday_batch.pgc - 연도별 공휴일 건수 배치 (XA).
 *
 * ATMI client: tpinit/tpopen open the ECPG XA RM, tpbegin starts ONE global
 * transaction, the EXEC SQL work runs on that branch, then tpcommit drives XA 2PC.
 * Usage: mm_holiday_batch [YEAR]
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <atmi.h>

int main(int argc, char **argv)
{
    long rows = 0; (void)rows; (void)argc; (void)argv;

    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt; char h_year[8];
    EXEC SQL END DECLARE SECTION;
    strncpy(h_year, (argc > 1) ? argv[1] : "2026", sizeof(h_year)-1); h_year[sizeof(h_year)-1] = 0;

    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(*) INTO :h_cnt FROM mm_holiday
        WHERE to_char(holiday_date, 'YYYY') = :h_year;
    if (sqlca.sqlcode < 0) { fprintf(stderr, "mm_holiday_batch FAIL [%d] %s\n", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); tpabort(0); return 1; }

    if (tpcommit(0) < 0) { fprintf(stderr, "tpcommit FAIL: %s\n", tpstrerror(tperrno)); tpabort(0); return 1; }
    printf(">>> mm_holiday_batch COMMIT: year=%s 공휴일=%ld\n", h_year, h_cnt);

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