package com.zioinfo.mall.gateway;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

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

/**
 * Mock 판매세 계산기 (기본). 외부 호출 0.
 * 주(state)별 대표 세율 테이블로 근사 계산한다(데모용).
 */
@Component
@ConditionalOnProperty(name = "mall.tax.provider", havingValue = "mock", matchIfMissing = true)
public class MockTaxCalculator implements TaxCalculator {

    private static final Map<String, BigDecimal> STATE_RATE = Map.ofEntries(
        Map.entry("NY", new BigDecimal("0.08875")), Map.entry("MA", new BigDecimal("0.0625")),
        Map.entry("IL", new BigDecimal("0.1025")),  Map.entry("CA", new BigDecimal("0.0725")),
        Map.entry("WA", new BigDecimal("0.1025")),  Map.entry("FL", new BigDecimal("0.07")),
        Map.entry("GA", new BigDecimal("0.089")),   Map.entry("TX", new BigDecimal("0.0825"))
    );

    @Override
    public Map<String, Object> calculate(BigDecimal amount, String zip, String state) {
        BigDecimal rate = STATE_RATE.getOrDefault(state == null ? "" : state.toUpperCase(), new BigDecimal("0.07"));
        BigDecimal tax = amount == null ? BigDecimal.ZERO
                : amount.multiply(rate).setScale(2, RoundingMode.HALF_UP);
        Map<String, Object> out = new LinkedHashMap<>();
        out.put("provider", "mock");
        out.put("rate", rate);
        out.put("taxAmount", tax);
        out.put("jurisdiction", (state == null ? "US" : state) + (zip == null ? "" : " " + zip));
        return out;
    }

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