package com.eactive.eai.custom.adapter.http.dynamic.filter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

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

import com.eactive.eai.adapter.http.dynamic.filter.FilterException;

class KakaopayFilterTest {

	private KakaopayFilter filter;

	@BeforeEach
	void setUp() {
		filter = new KakaopayFilter();
	}

	// -----------------------------------------------------------------------
	// doPreFilter
	// -----------------------------------------------------------------------

	@Test
	void preFilter_firstAgreedTrue_returnsMessage() throws Exception {
		String message = "{\"term_agreements\":[{\"is_agreed\":true}]}";

		Object result = filter.doPreFilter(null, null, message, null, null, null);

		assertSame(message, result);
	}

	@Test
	void preFilter_firstAgreedFalse_throwsFilterException() throws Exception {
		String message = "{\"term_agreements\":[{\"is_agreed\":false}]}";

		FilterException ex = assertThrows(FilterException.class,
				() -> filter.doPreFilter(null, null, message, null, null, null));
		assertEquals("EB000003", ex.getMessage());
		assertEquals(200, ex.getStatus());
	}

	@Test
	void preFilter_isAgreedFieldMissing_throwsFilterException() {
		// is_agreed 필드 없음 → getBoolean 기본값 false → 예외
		String message = "{\"term_agreements\":[{}]}";

		assertThrows(FilterException.class,
				() -> filter.doPreFilter(null, null, message, null, null, null));
	}

	@Test
	void preFilter_termAgreementsEmpty_throwsFilterException() {
		// 배열이 비어있어 [0] 접근 불가 → getBoolean 기본값 false → 예외
		String message = "{\"term_agreements\":[]}";

		assertThrows(FilterException.class,
				() -> filter.doPreFilter(null, null, message, null, null, null));
	}

	@Test
	void preFilter_termAgreementsMissing_throwsFilterException() {
		// term_agreements 필드 자체 없음 → getBoolean 기본값 false → 예외
		String message = "{}";

		assertThrows(FilterException.class,
				() -> filter.doPreFilter(null, null, message, null, null, null));
	}

	@Test
	void preFilter_multipleEntries_firstAgreedTrue_returnsMessage() throws Exception {
		// 첫 번째 true, 두 번째 false → 통과
		String message = "{\"term_agreements\":["
				+ "{\"is_agreed\":true},"
				+ "{\"is_agreed\":false}"
				+ "]}";

		Object result = filter.doPreFilter(null, null, message, null, null, null);

		assertSame(message, result);
	}

	@Test
	void preFilter_multipleEntries_firstAgreedFalse_throwsFilterException() {
		// 첫 번째 false, 두 번째 true → 예외
		String message = "{\"term_agreements\":["
				+ "{\"is_agreed\":false},"
				+ "{\"is_agreed\":true}"
				+ "]}";

		assertThrows(FilterException.class,
				() -> filter.doPreFilter(null, null, message, null, null, null));
	}

	// -----------------------------------------------------------------------
	// doPostFilter
	// -----------------------------------------------------------------------

	@Test
	void postFilter_alwaysReturnsResultMessage() throws Exception {
		String resultMessage = "{\"result\":\"ok\"}";

		Object result = filter.doPostFilter(null, null, resultMessage, null, null, null);

		assertSame(resultMessage, result);
	}
}
