package com.zioinfo.mall.gateway;

import com.zioinfo.mall.common.CryptoUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Mock 결제 게이트웨이 (기본). 외부 호출 0 — 개발/데모용.
 * 항상 승인 처리하고 가짜 txnId를 발급한다.
 */
@Component
@ConditionalOnProperty(name = "mall.payment.provider", havingValue = "mock", matchIfMissing = true)
public class MockPaymentGateway implements PaymentGateway {

    @Override
    public Map<String, Object> charge(String orderNo, BigDecimal amount, String currency, String cardToken) {
        Map<String, Object> out = new LinkedHashMap<>();
        out.put("provider", "mock");
        out.put("txnId", "MOCK-" + UUID.randomUUID().toString().substring(0, 12));
        out.put("status", "APPROVED");
        out.put("amount", amount);
        out.put("currency", currency == null ? "USD" : currency);
        out.put("cardMask", CryptoUtil.maskTail(cardToken));
        return out;
    }

    @Override
    public Map<String, Object> refund(String txnId, BigDecimal amount) {
        Map<String, Object> out = new LinkedHashMap<>();
        out.put("provider", "mock");
        out.put("txnId", txnId);
        out.put("status", "REFUNDED");
        out.put("amount", amount);
        return out;
    }

    @Override
    public String provider() {
        return "mock";
    }
}
