package com.ga.external.push;

import com.ga.common.adapter.push.PushAdapter;
import com.ga.common.adapter.push.PushRequest;
import com.ga.common.adapter.push.PushResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 푸시 Mock 구현 (기본).
 * 활성 조건: app.adapter.push=mock (기본). 실제 FCM 통합 시 yml 값을 'fcm' 으로 변경.
 * 항상 sent=true 반환, 토큰은 앞부분만 로그.
 */
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app.adapter", name = "push", havingValue = "mock", matchIfMissing = true)
public class MockPushAdapter implements PushAdapter {

    @Override
    public PushResponse send(PushRequest request) {
        String id = "MOCK-PUSH-" + UUID.randomUUID();
        log.info("[MOCK PUSH] token={} title={} refType={} refId={} id={}",
                maskToken(request.getDeviceToken()), request.getTitle(),
                request.getRefType(), request.getRefId(), id);
        return PushResponse.builder()
                .messageId(id).sent(true)
                .resultCode("0000").resultMessage("MOCK sent")
                .sentAt(LocalDateTime.now()).build();
    }

    private String maskToken(String token) {
        if (token == null) return null;
        return token.length() <= 8 ? "****" : token.substring(0, 8) + "****";
    }
}
