/*
 * py_payout_dbio.pgc - 지급(payout)/조정/집계 DB 접근 (libpydbio.a).
 * ECPG functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 */
#include <string.h>
#include <userlog.h>
#include "py_dbio.h"

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

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

int pydb_insert_payout(long payout_id, const char *merch, const char *acct, long amount,
                       long fee, long net, const char *status, const char *channel,
                       const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = payout_id, h_amount = amount, h_fee = fee, h_net = net;
    char h_merch[64], h_acct[32], h_status[8], h_channel[16], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_merch, merch, sizeof(h_merch) - 1);       h_merch[sizeof(h_merch)-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;
    strncpy(h_channel, channel, sizeof(h_channel) - 1); h_channel[sizeof(h_channel)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate) - 1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    EXEC SQL INSERT INTO py_payout
        (payout_id, merchant_id, account_no, amount, fee, net, status, channel, biz_date)
        VALUES (:h_id, :h_merch, :h_acct, :h_amount, :h_fee, :h_net, :h_status, :h_channel, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("pydb_insert_payout FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int pydb_update_status(long payout_id, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = payout_id;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

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

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

int pydb_get_status(long payout_id, char *out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = payout_id;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

    EXEC SQL SELECT status INTO :h_status FROM py_payout WHERE payout_id = :h_id;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("pydb_get_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    out[0] = h_status[0];
    out[1] = 0;
    return 0;
}

int pydb_set_file(long payout_id, long file_id, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = payout_id, h_file = file_id;
    char h_status[8];
    EXEC SQL END DECLARE SECTION;

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

    EXEC SQL UPDATE py_payout SET file_id = :h_file, status = :h_status, updated_at = now()
        WHERE payout_id = :h_id;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("pydb_set_file FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

int pydb_upsert_summary(const char *merch, const char *bizdate, long cnt, long amount)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_cnt = cnt, h_amt = amount;
    char h_merch[64], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

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

    EXEC SQL INSERT INTO py_summary (merchant_id, biz_date, payout_count, payout_amount)
        VALUES (:h_merch, :h_bizdate, :h_cnt, :h_amt)
        ON CONFLICT (merchant_id, biz_date) DO UPDATE
        SET payout_count = py_summary.payout_count + EXCLUDED.payout_count,
            payout_amount = py_summary.payout_amount + EXCLUDED.payout_amount,
            updated_at = now();
    if (sqlca.sqlcode < 0) {
        userlog("pydb_upsert_summary FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

long pydb_insert_adjust(long payout_id, long delta, const char *reason, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id, h_pay = payout_id, h_delta = delta;
    char h_reason[128], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_reason, reason, sizeof(h_reason) - 1);   h_reason[sizeof(h_reason)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate) - 1); h_bizdate[sizeof(h_bizdate)-1] = 0;

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

    EXEC SQL INSERT INTO py_adjust (adj_id, payout_id, delta, reason, biz_date)
        VALUES (:h_id, :h_pay, :h_delta, :h_reason, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("pydb_insert_adjust FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return h_id;
}
