/* @tier medium @module lg @transform ledger-online */
/*
 * lg_ol_0029.pgc - 원장 온라인 서비스 (LG_OL_0029)  [tier=medium]
 *
 * 역분개 승인: 접수(R) 상태의 역분개행(금액이 음수인 반대분개)을 확인하고
 * 승인 처리(M) 로 전이한다. 역분개가 아니거나 상태가 다르면 거부 응답한다.
 */
#include <stdio.h>
#include <string.h>

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

EXEC SQL INCLUDE sqlca;

TX_SERVICE(LG_OL_0029, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_key[16];
    char h_status_r[2];
    char h_status_m[2];
    char h_cur_status[2];
    long h_amount;
    EXEC SQL END DECLARE SECTION;

    char revkey[16];
    long approved;

    tx_log(TX_LOG_INFO, "[LG_OL_0029] 역분개 승인 진입");

    if (tx_buf_get(ctx->in, "REVKEY", revkey, sizeof(revkey)) != TX_OK ||
        revkey[0] == '\0') {
        tx_log(TX_LOG_ERROR, "[LG_OL_0029] REVKEY 누락");
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    strncpy(h_key, revkey, sizeof(h_key) - 1);
    h_key[sizeof(h_key) - 1] = '\0';
    strncpy(h_status_r, LG_ST_RECV, sizeof(h_status_r) - 1);
    h_status_r[sizeof(h_status_r) - 1] = '\0';
    strncpy(h_status_m, LG_ST_DONE, sizeof(h_status_m) - 1);
    h_status_m[sizeof(h_status_m) - 1] = '\0';

    EXEC SQL SELECT amount, status
        INTO :h_amount, :h_cur_status
        FROM lg_ledger WHERE key = :h_key;
    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[LG_OL_0029] 역분개행 없음 revkey=%s", revkey);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_NOMERCH);
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("lg_ol_0029 select");
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_SYSERR);
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    /* 역분개 조건: 접수(R) 상태 + 음수 금액 */
    if (h_cur_status[0] != LG_ST_RECV[0] || h_amount >= 0) {
        tx_log(TX_LOG_WARN, "[LG_OL_0029] 역분개 승인 거부 revkey=%s status=%s amount=%ld",
               revkey, h_cur_status, h_amount);
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID);
        tx_buf_sets(ctx->out, "STATUS", h_cur_status);
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    if (tx_begin() != TX_OK) {
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    /* R + 음수 조건부 승인 전이 */
    EXEC SQL UPDATE lg_ledger
        SET status = :h_status_m, upd_ts = now()
        WHERE key = :h_key
          AND status = :h_status_r
          AND amount < 0;
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("lg_ol_0029 update");
        tx_abort();
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_SYSERR);
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }
    approved = (long)sqlca.sqlerrd[2];

    if (approved == 0) {
        tx_log(TX_LOG_WARN, "[LG_OL_0029] 승인 대상 없음 revkey=%s", revkey);
        tx_abort();
        tx_buf_reset(ctx->out);
        tx_buf_sets(ctx->out, "RESPCODE", RESP_INVALID);
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    if (tx_commit() != TX_OK) {
        tx_abort();
        tx_return(ctx, TX_FAIL, ctx->out);
        return;
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "RESPCODE", RESP_OK);
    tx_buf_sets(ctx->out, "REVKEY", revkey);
    tx_buf_setlong(ctx->out, "REVAMT", h_amount);
    tx_buf_sets(ctx->out, "STATUS", LG_ST_DONE);
    tx_log(TX_LOG_INFO, "[LG_OL_0029] 역분개 승인완료 revkey=%s 금액=%ld", revkey, h_amount);
    tx_return(ctx, TX_OK, ctx->out);
}
