/*
 * mm_merch_dbio.pgc - 가맹점 확장 마스터 CRUD + 변경 이력 (libmmdbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "mm_dbio.h"

long mmdb_next_hist_id(void)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id;
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT nextval('mm_merch_hist_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) {
        userlog("mmdb_next_hist_id FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}

int mmdb_insert_merchant(const char *merch, const char *name, const char *grade,
                         const char *acct, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_name[128], h_grade[8], h_acct[64], h_status[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_merch,  merch,  sizeof(h_merch) - 1);  h_merch[sizeof(h_merch)-1]  = 0;
    strncpy(h_name,   name,   sizeof(h_name) - 1);   h_name[sizeof(h_name)-1]    = 0;
    strncpy(h_grade,  grade,  sizeof(h_grade) - 1);  h_grade[sizeof(h_grade)-1]  = 0;
    strncpy(h_acct,   acct,   sizeof(h_acct) - 1);   h_acct[sizeof(h_acct)-1]    = 0;
    strncpy(h_status, status, sizeof(h_status) - 1); h_status[sizeof(h_status)-1]= 0;

    EXEC SQL INSERT INTO mm_merchant_ext
        (merchant_id, name, grade, settle_acct, status)
        VALUES (:h_merch, :h_name, :h_grade, :h_acct, :h_status)
        ON CONFLICT (merchant_id) DO UPDATE
        SET name = EXCLUDED.name, grade = EXCLUDED.grade,
            settle_acct = EXCLUDED.settle_acct, status = EXCLUDED.status,
            updated_at = now();
    if (sqlca.sqlcode < 0) {
        userlog("mmdb_insert_merchant FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int mmdb_get_merchant(const char *merch, char *name_out, char *grade_out,
                      char *status_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_name[128], h_grade[8], h_status[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_merch, merch, sizeof(h_merch) - 1); h_merch[sizeof(h_merch)-1] = 0;

    EXEC SQL SELECT name, grade, status INTO :h_name, :h_grade, :h_status
        FROM mm_merchant_ext WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("mmdb_get_merchant FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    strncpy(name_out,   h_name,   63);  name_out[63]   = 0;
    strncpy(grade_out,  h_grade,  7);   grade_out[7]   = 0;
    strncpy(status_out, h_status, 15);  status_out[15] = 0;
    return 0;
}

int mmdb_update_merchant_status(const char *merch, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_status[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_merch,  merch,  sizeof(h_merch) - 1);  h_merch[sizeof(h_merch)-1]  = 0;
    strncpy(h_status, status, sizeof(h_status) - 1); h_status[sizeof(h_status)-1]= 0;

    EXEC SQL UPDATE mm_merchant_ext SET status = :h_status, updated_at = now()
        WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("mmdb_update_merchant_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int mmdb_update_merchant_grade(const char *merch, const char *grade)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_merch[64], h_grade[8];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_merch, merch, sizeof(h_merch) - 1); h_merch[sizeof(h_merch)-1] = 0;
    strncpy(h_grade, grade, sizeof(h_grade) - 1); h_grade[sizeof(h_grade)-1] = 0;

    EXEC SQL UPDATE mm_merchant_ext SET grade = :h_grade, updated_at = now()
        WHERE merchant_id = :h_merch;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("mmdb_update_merchant_grade FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int mmdb_insert_merch_hist(const char *merch, const char *action,
                           const char *before_val, const char *after_val,
                           const char *memo)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id;
    char h_merch[64], h_action[16], h_before[64], h_after[64], h_memo[128];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_merch,  merch,      sizeof(h_merch) - 1);  h_merch[sizeof(h_merch)-1]  = 0;
    strncpy(h_action, action,     sizeof(h_action) - 1); h_action[sizeof(h_action)-1]= 0;
    strncpy(h_before, before_val, sizeof(h_before) - 1); h_before[sizeof(h_before)-1]= 0;
    strncpy(h_after,  after_val,  sizeof(h_after) - 1);  h_after[sizeof(h_after)-1]  = 0;
    strncpy(h_memo,   memo,       sizeof(h_memo) - 1);   h_memo[sizeof(h_memo)-1]    = 0;

    EXEC SQL SELECT nextval('mm_merch_hist_seq') INTO :h_id;
    if (sqlca.sqlcode < 0) return -1;

    EXEC SQL INSERT INTO mm_merch_hist
        (hist_id, merchant_id, action, before_val, after_val, memo)
        VALUES (:h_id, :h_merch, :h_action, :h_before, :h_after, :h_memo);
    if (sqlca.sqlcode < 0) {
        userlog("mmdb_insert_merch_hist FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}
