package com.caliverse.admin.dynamodb.domain;

import com.caliverse.admin.dynamodb.domain.doc.DynamoDBDocBase;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbAttribute;

import java.lang.reflect.Method;

public class DocAttributeHandler {
    public static String getAttribFieldName(DynamoDBDocBase doc) {
        try {
            Method method = doc.getClass().getMethod("getAttribFieldName");
            return (String) method.invoke(doc);
        } catch (Exception e) {
            // getAttribFieldName 메서드가 없는 경우, 클래스명에서 유추
            String className = doc.getClass().getSimpleName();
            return className.replace("Doc", "Attrib");
        }
    }

    public static Object getAttribValue(DynamoDBDocBase doc) {
        try {
            Method method = doc.getClass().getMethod("getAttribValue");
            return method.invoke(doc);
        } catch (Exception e) {
            return null;
        }
    }

    public static String findAttribAnnotationValue(DynamoDBDocBase doc) {
        try {
            Method method = doc.getClass().getMethod("getAttribValue");
            DynamoDbAttribute annotation = method.getAnnotation(DynamoDbAttribute.class);
            if (annotation != null) {
                return annotation.value();
            }
        } catch (Exception e) {
            // 무시
        }
        return getAttribFieldName(doc);
    }
}
