/* @tier easy @module py @transform payment-online */
/*
 * py_ol_0019.pgc - 지급 계좌조회(등록 계좌/가맹점 확인) (PY_OL_0019)  [tier=easy]
 *
 * TxCore TX_SERVICE 엔트리. 요청 TXBUF 를 검증하고 임베디드 SQL / 표준
 * DBIO / tx_call 로 처리한 뒤 응답 TXBUF 를 구성한다.
 */
#include <stdio.h>
#include <string.h>

#include "txcore.h"
#include "txcore_dbio.h"
#include "acq_util.h"
#include "py_core.h"

EXEC SQL INCLUDE sqlca;

TX_SERVICE(PY_OL_0019, ctx)
{
    char key[16];
    if (tx_buf_get(ctx->in, "KEY", key, sizeof(key)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[PY_OL_0019] KEY 누락");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16], h_acct[20], h_merch[16];
    EXEC SQL END DECLARE SECTION;

    char masked[20];
    int  i;

    tx_log(TX_LOG_INFO, "[PY_OL_0019] 지급 계좌조회 진입");
    strncpy(h_key, key, 15); h_key[15] = '\0';

    EXEC SQL SELECT card_no, merch_id INTO :h_acct, :h_merch
        FROM py_ledger WHERE key = :h_key;
    if (sqlca.sqlcode == TX_SQL_NOTFOUND) { tx_return(ctx, TX_ENOENT, ctx->out); return; }

    /* 계좌 마스킹 (뒤 4자리만 노출) */
    strncpy(masked, h_acct, sizeof(masked) - 1);
    masked[sizeof(masked) - 1] = '\0';
    for (i = 0; masked[i] && i < (int)strlen(masked) - 4; i++)
        if (masked[i] != '-') masked[i] = '*';

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "KEY", key);
    tx_buf_sets(ctx->out, "MERCHID", h_merch);
    tx_buf_sets(ctx->out, "ACCTMASK", masked);
    tx_log(TX_LOG_INFO, "[PY_OL_0019] 계좌조회 key=%s merch=%s", key, h_merch);
    tx_return(ctx, TX_OK, ctx->out);
}
