/* @tier easy @module vl @transform validation-online */
/*
 * vl_ol_0016.pgc - 전문 포맷 검증 서비스 (VL_OL_0016)
 *
 * 입력 RAW 전문을 고정길이 레이아웃(acq_msg_t) 으로 언팩하여 길이/필수필드
 * (msg_validate) 를 검증하고, 전문유형코드(msg_type) 를 전문유형 마스터
 * (vl_msg_type) 에서 조회하여 등록/사용 여부를 확인한다. 실패 시 불일치(VL16xx).
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

EXEC SQL INCLUDE sqlca;

TX_SERVICE(VL_OL_0016, ctx)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_msg_type[5];
    char h_use_yn[2];
    char h_type_name[41];
    EXEC SQL END DECLARE SECTION;

    acq_msg_t msg;
    char raw[128];
    int  rawlen;

    tx_log(TX_LOG_INFO, "[VL_OL_0016] 전문 포맷 검증 서비스 진입");

    if (tx_buf_get(ctx->in, "RAW", raw, sizeof(raw)) != TX_OK) {
        tx_log(TX_LOG_ERROR, "[VL_OL_0016] RAW 전문 누락");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1600");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 1) 길이 검증 */
    rawlen = (int)strlen(raw);
    if (rawlen < MSG_ACQ_LEN) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 전문 길이 부족 len=%d need=%d",
               rawlen, MSG_ACQ_LEN);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1601");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 2) 언팩 + 필수필드 검증 */
    memset(&msg, 0, sizeof(msg));
    if (msg_unpack(&msg, raw, rawlen) != 0 || msg_validate(&msg) != 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 전문 검증 실패(msg_validate)");
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1602");
        tx_return(ctx, TX_EINVAL, ctx->out);
        return;
    }

    /* 3) 전문유형코드 유효성 조회 */
    memset(h_msg_type, 0, sizeof(h_msg_type));
    memcpy(h_msg_type, msg.msg_type, FLD_MSG_TYPE_LEN);
    h_msg_type[FLD_MSG_TYPE_LEN] = '\0';
    memset(h_use_yn, 0, sizeof(h_use_yn));
    memset(h_type_name, 0, sizeof(h_type_name));

    EXEC SQL
        SELECT use_yn, type_name
          INTO :h_use_yn, :h_type_name
          FROM vl_msg_type
         WHERE msg_type = :h_msg_type;

    if (sqlca.sqlcode == TX_SQL_NOTFOUND) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 미등록 전문유형 type=%s", h_msg_type);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1603");
        tx_return(ctx, TX_ENOENT, ctx->out);
        return;
    }
    if (sqlca.sqlcode != TX_SQL_OK) {
        TX_DBIO_LOG_ERR("SELECT vl_msg_type");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1609");
        tx_return(ctx, TX_EDB, ctx->out);
        return;
    }

    tx_buf_reset(ctx->out);
    tx_buf_sets(ctx->out, "MSGTYPE", h_msg_type);
    tx_buf_sets(ctx->out, "TYPENAME", h_type_name);
    tx_buf_sets(ctx->out, "USEYN", h_use_yn);
    tx_buf_setlong(ctx->out, "RAWLEN", (long)rawlen);

    if (memcmp(h_use_yn, "Y", 1) != 0) {
        tx_log(TX_LOG_WARN, "[VL_OL_0016] 비활성 전문유형 type=%s use=%s",
               h_msg_type, h_use_yn);
        tx_buf_sets(ctx->out, "RESULT", "MISMATCH");
        tx_buf_sets(ctx->out, "ERRCODE", "VL1604");
        tx_return(ctx, TX_OK, ctx->out);
        return;
    }

    tx_buf_sets(ctx->out, "RESULT", "MATCH");
    tx_buf_sets(ctx->out, "ERRCODE", RESP_OK);
    tx_log(TX_LOG_INFO, "[VL_OL_0016] 전문 포맷 정상 type=%s name=%s",
           h_msg_type, h_type_name);
    tx_return(ctx, TX_OK, ctx->out);
}
