package com.zioinfo.mall.gateway;

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

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Mock 주소 검증기 (기본). 외부 호출 0.
 * ZIP 5자리 형식이면 valid 처리, 라우팅은 입력 순서를 유지하며 가짜 ETA 부여.
 */
@Component
@ConditionalOnProperty(name = "mall.address.provider", havingValue = "mock", matchIfMissing = true)
public class MockAddressVerifier implements AddressVerifier {

    @Override
    public Map<String, Object> verify(String address, String zip) {
        boolean valid = zip != null && zip.matches("\\d{5}") && address != null && !address.isBlank();
        Map<String, Object> out = new LinkedHashMap<>();
        out.put("provider", "mock");
        out.put("valid", valid);
        out.put("normalized", address == null ? "" : address.trim());
        out.put("lat", 40.0 + (zip == null ? 0 : (zip.hashCode() % 1000) / 10000.0));
        out.put("lng", -74.0 - (zip == null ? 0 : (Math.abs(zip.hashCode()) % 1000) / 10000.0));
        return out;
    }

    @Override
    public Map<String, Object> route(List<String> addresses, String origin) {
        List<Map<String, Object>> ordered = new ArrayList<>();
        int seq = 1;
        for (String a : addresses) {
            Map<String, Object> stop = new LinkedHashMap<>();
            stop.put("seq", seq);
            stop.put("address", a);
            stop.put("lat", 40.0 + seq * 0.01);
            stop.put("lng", -74.0 - seq * 0.01);
            stop.put("eta", (seq * 15) + " min");
            ordered.add(stop);
            seq++;
        }
        Map<String, Object> out = new LinkedHashMap<>();
        out.put("provider", "mock");
        out.put("origin", origin);
        out.put("ordered", ordered);
        return out;
    }

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