package com.eactive.eai.custom.message;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.eactive.eai.message.StandardItem;
import com.eactive.eai.message.StandardMessage;
import com.eactive.eai.message.StandardMessageUtil;

/**
 * standard-layout-djb.csv 로드 및 전문 구조 검증
 */
class StandardLayoutDJBTest {

    private StandardMessage message;

    @BeforeEach
    void setUp() {
        message = StandardMessageUtil.generateMessageFromCsvFile(
                "src/main/resources/standard-layout-djb.csv");
        assertNotNull(message, "standard-layout-djb.csv 로드 실패");
        message.setReadPosition(0);
    }

    // ----------------------------------------------------------------
    // 최상위 블록 존재 여부
    // ----------------------------------------------------------------

    @Test
    void HEAD_블록_존재() {
        assertNotNull(message.findItem("HEAD"), "HEAD 블록 없음");
    }

    @Test
    void EZDATA_블록_존재() {
        assertNotNull(message.findItem("EZDATA"), "EZDATA 블록 없음");
    }

    @Test
    void MSG_블록_존재() {
        assertNotNull(message.findItem("MSG"), "MSG 블록 없음");
    }

    @Test
    void DATA_블록_존재() {
        assertNotNull(message.findItem("DATA"), "DATA 블록 없음");
    }

    // ----------------------------------------------------------------
    // HEAD 핵심 필드 검증
    // ----------------------------------------------------------------

    @Test
    void HEAD_guid_필드_존재_및_길이40() {
        StandardItem guidItem = message.findItem("HEAD.guid");
        assertNotNull(guidItem, "HEAD.guid 필드 없음");
        assertEquals(40, guidItem.getLength(), "HEAD.guid 길이는 40자여야 함");
    }

    @Test
    void HEAD_guid_prgs_no_필드_존재() {
        StandardItem item = message.findItem("HEAD.guid_prgs_no");
        assertNotNull(item, "HEAD.guid_prgs_no 필드 없음");
        assertEquals(3, item.getLength());
    }

    @Test
    void HEAD_nxgn_stnd_idfr_기본값_JERA() {
        StandardItem item = message.findItem("HEAD.nxgn_stnd_idfr");
        assertNotNull(item, "HEAD.nxgn_stnd_idfr 필드 없음");
        assertEquals(4, item.getLength());
        assertEquals("JERA", item.getValue());
    }

    @Test
    void HEAD_ortr_guid_필드_존재_및_길이40() {
        StandardItem item = message.findItem("HEAD.ortr_guid");
        assertNotNull(item, "HEAD.ortr_guid 필드 없음");
        assertEquals(40, item.getLength());
    }

    @Test
    void HEAD_if_id_필드_길이30() {
        StandardItem item = message.findItem("HEAD.if_id");
        assertNotNull(item, "HEAD.if_id 필드 없음");
        assertEquals(30, item.getLength(), "HEAD.if_id 길이는 30자여야 함 (기존 20자에서 변경)");
    }

    @Test
    void HEAD_dman_rspn_dvcd_기본값_S() {
        StandardItem item = message.findItem("HEAD.dman_rspn_dvcd");
        assertNotNull(item, "HEAD.dman_rspn_dvcd 필드 없음");
        assertEquals("S", item.getValue());
    }

    @Test
    void HEAD_procs_rslt_dvcd_기본값_S() {
        StandardItem item = message.findItem("HEAD.procs_rslt_dvcd");
        assertNotNull(item, "HEAD.procs_rslt_dvcd 필드 없음");
        assertEquals("S", item.getValue());
    }

    @Test
    void HEAD_trnm_sys_dvcd_기본값_EAI() {
        StandardItem item = message.findItem("HEAD.trnm_sys_dvcd");
        assertNotNull(item, "HEAD.trnm_sys_dvcd 필드 없음");
        assertEquals("EAI", item.getValue());
    }

    // ----------------------------------------------------------------
    // MSG 영역 구조 검증
    // ----------------------------------------------------------------

    @Test
    void MSG_MAIN_MSG_블록_존재() {
        assertNotNull(message.findItem("MSG.MAIN_MSG"), "MSG.MAIN_MSG 블록 없음");
    }

    @Test
    void MSG_outp_msg_cd_길이12() {
        StandardItem item = message.findItem("MSG.MAIN_MSG.outp_msg_cd");
        assertNotNull(item, "MSG.MAIN_MSG.outp_msg_cd 필드 없음");
        assertEquals(12, item.getLength());
    }

    @Test
    void MSG_MSG_LIST_Grid_존재() {
        assertNotNull(message.findItem("MSG.MSG_LIST"), "MSG.MSG_LIST Grid 없음");
    }

    // ----------------------------------------------------------------
    // DATA 영역 구조 검증
    // ----------------------------------------------------------------

    @Test
    void DATA_BIZ_DATA_존재() {
        assertNotNull(message.findItem("DATA.BIZ_DATA"), "DATA.BIZ_DATA 없음");
    }

    // ----------------------------------------------------------------
    // setData / findItemValue 기본 동작
    // ----------------------------------------------------------------

    @Test
    void HEAD_필드_setData_findItemValue() {
        String guid40 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; // 40자
        message.setData("HEAD.guid", guid40);
        assertEquals(guid40, message.findItemValue("HEAD.guid"));
    }

    @Test
    void MSG_필드_setData_findItemValue() {
        message.setData("MSG.MAIN_MSG.outp_msg_ctnt", "테스트오류메시지");
        assertEquals("테스트오류메시지", message.findItemValue("MSG.MAIN_MSG.outp_msg_ctnt"));
    }
}
