/*
 * mg_route_dbio.pgc - 라우팅 룰 + 채널 세션 DB 접근 (part of libmgdbio.a).
 */
#include <string.h>
#include <userlog.h>
#include "mg_dbio.h"

int mgdb_insert_route(long route_id, const char *channel, const char *mti,
                      const char *dest, long priority, const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_id = route_id, h_pri = priority;
    char h_chan[16], h_mti[8], h_dest[64], h_bizdate[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_chan, channel, sizeof(h_chan)-1);       h_chan[sizeof(h_chan)-1] = 0;
    strncpy(h_mti, mti, sizeof(h_mti)-1);             h_mti[sizeof(h_mti)-1] = 0;
    strncpy(h_dest, dest, sizeof(h_dest)-1);          h_dest[sizeof(h_dest)-1] = 0;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;

    EXEC SQL INSERT INTO mg_route (route_id, channel, mti, dest, priority, active, biz_date)
        VALUES (:h_id, :h_chan, :h_mti, :h_dest, :h_pri, true, :h_bizdate);
    if (sqlca.sqlcode < 0) {
        userlog("mgdb_insert_route FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}

/* 채널+전문유형으로 활성 라우팅 목적지를 우선순위 순으로 1건 조회. */
int mgdb_lookup_route(const char *channel, const char *mti, char *dest_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_chan[16], h_mti[8], h_dest[64];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_chan, channel, sizeof(h_chan)-1); h_chan[sizeof(h_chan)-1] = 0;
    strncpy(h_mti, mti, sizeof(h_mti)-1);       h_mti[sizeof(h_mti)-1] = 0;

    EXEC SQL SELECT dest INTO :h_dest FROM mg_route
        WHERE channel = :h_chan AND mti = :h_mti AND active = true
        ORDER BY priority ASC LIMIT 1;
    if (sqlca.sqlcode == 100) { dest_out[0] = 0; return -1; }
    if (sqlca.sqlcode < 0) {
        userlog("mgdb_lookup_route FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    strncpy(dest_out, h_dest, 63); dest_out[63] = 0;
    return 0;
}

int mgdb_get_channel_status(const char *channel, char *status_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_chan[16], h_status[16];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_chan, channel, sizeof(h_chan)-1); h_chan[sizeof(h_chan)-1] = 0;

    EXEC SQL SELECT status INTO :h_status FROM mg_channel WHERE channel = :h_chan;
    if (sqlca.sqlcode == 100) { strcpy(status_out, "UNKNOWN"); return -1; }
    if (sqlca.sqlcode < 0) {
        userlog("mgdb_get_channel_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    strncpy(status_out, h_status, 15); status_out[15] = 0;
    return 0;
}

int mgdb_set_channel_status(const char *channel, const char *status)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_chan[16], h_status[16];
    EXEC SQL END DECLARE SECTION;

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

    EXEC SQL UPDATE mg_channel SET status = :h_status, updated_at = now()
        WHERE channel = :h_chan;
    if (sqlca.sqlcode < 0) {
        userlog("mgdb_set_channel_status FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    if (sqlca.sqlcode == 100) {   /* 신규 채널 등록 */
        EXEC SQL INSERT INTO mg_channel (channel, status) VALUES (:h_chan, :h_status);
        if (sqlca.sqlcode < 0) return -1;
    }
    return 0;
}

int mgdb_set_channel_session(const char *channel, const char *session_key, long stan)
{
    EXEC SQL BEGIN DECLARE SECTION;
    long h_stan = stan;
    char h_chan[16], h_key[64];
    EXEC SQL END DECLARE SECTION;

    strncpy(h_chan, channel, sizeof(h_chan)-1);  h_chan[sizeof(h_chan)-1] = 0;
    strncpy(h_key, session_key, sizeof(h_key)-1); h_key[sizeof(h_key)-1] = 0;

    EXEC SQL UPDATE mg_channel
        SET session_key = :h_key, last_stan = :h_stan, status = 'SIGNON',
            signon_at = now(), updated_at = now()
        WHERE channel = :h_chan;
    if (sqlca.sqlcode < 0 || sqlca.sqlcode == 100) {
        userlog("mgdb_set_channel_session FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc);
        return -1;
    }
    return 0;
}
