package com.ga.common.util;

import com.ga.common.exception.BizException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class EncryptUtilTest {

    private final EncryptUtil util = new EncryptUtil("ga-test-key-1234567890-abcdefghi");

    @Test
    @DisplayName("encrypt → decrypt round-trip")
    void roundTrip() {
        String plain = "9001011234567"; // 주민번호 형태
        String enc = util.encrypt(plain);

        assertThat(enc).isNotEqualTo(plain);
        assertThat(enc).isBase64();
        assertThat(util.decrypt(enc)).isEqualTo(plain);
    }

    @Test
    @DisplayName("동일 평문 → 동일 암호문 (고정 IV)")
    void deterministic() {
        String enc1 = util.encrypt("hello");
        String enc2 = util.encrypt("hello");
        assertThat(enc1).isEqualTo(enc2);
    }

    @Test
    @DisplayName("null/empty 는 그대로 반환")
    void nullEmpty() {
        assertThat(util.encrypt(null)).isNull();
        assertThat(util.encrypt("")).isEmpty();
        assertThat(util.decrypt(null)).isNull();
        assertThat(util.decrypt("")).isEmpty();
    }

    @Test
    @DisplayName("키가 32바이트 미만이면 0 패딩 → 정상 동작")
    void shortKey() {
        EncryptUtil shortKey = new EncryptUtil("short");
        String enc = shortKey.encrypt("data");
        assertThat(shortKey.decrypt(enc)).isEqualTo("data");
    }

    @Test
    @DisplayName("잘못된 암호문 복호화 시 BizException")
    void decryptInvalid() {
        assertThatThrownBy(() -> util.decrypt("not-base64-!!"))
                .isInstanceOf(BizException.class);
    }

    @Test
    @DisplayName("한글/특수문자 round-trip")
    void multiByte() {
        String plain = "홍길동@한국#$% áéíóú";
        assertThat(util.decrypt(util.encrypt(plain))).isEqualTo(plain);
    }
}
