package com.caliverse.admin.domain.cache;

import com.caliverse.admin.domain.request.LogGenericRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.cache.interceptor.KeyGenerator;
import java.lang.reflect.Method;

@Component("logCountKeyGenerator")
@Slf4j
public class LogCountKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        LogGenericRequest request = (LogGenericRequest) params[0];

        StringBuilder keyBuilder = new StringBuilder();
        keyBuilder.append(request.getStartDt().toLocalDate())
                .append("_")
                .append(request.getEndDt().toLocalDate())
                .append("_")
                .append(request.getLogAction() != null ? request.getLogAction().name() : "ALL")
                .append("_")
                .append(request.getLogDomain() != null ? request.getLogDomain().name() : "ALL")
                .append("_")
                .append(request.getSearchData() != null ? request.getSearchData() : "ALL")
                .append("_")
                .append(request.getTranId() != null ? request.getTranId() : "ALL");

        // 필터가 있으면 해시 추가
        if (request.getFilters() != null && !request.getFilters().isEmpty()) {
            keyBuilder.append("_").append(request.getFilters().hashCode());
        }

        String finalKey = keyBuilder.toString();
        log.info("Generated cache key: '{}'", finalKey);

        return finalKey;
    }

}
