package com.ruoyi.app.flashdelivery.route;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class FlashDeliveryRouteServiceTest {

    @Test
    void keepsDrivingRouteWhenProviderSucceeds() {
        RouteDistanceProvider provider = (a, b, c, d) -> new RouteDistance(5210, 780, "ROUTE");
        FlashDeliveryRouteService service = new FlashDeliveryRouteService(provider);

        RouteDistance result = service.calculate(point("25.0330", "121.5645"),
                point("25.0478", "121.5319"));

        assertEquals(5210, result.distanceMeters());
        assertEquals(780, result.durationSeconds());
        assertEquals("ROUTE", result.source());
    }

    @Test
    void fallsBackToStraightLineWhenProviderFails() {
        RouteDistanceProvider provider = (a, b, c, d) -> { throw new IllegalStateException("down"); };
        FlashDeliveryRouteService service = new FlashDeliveryRouteService(provider);

        RouteDistance result = service.calculate(point("25.0330", "121.5645"),
                point("25.0478", "121.5319"));

        assertTrue(result.distanceMeters() >= 3600 && result.distanceMeters() <= 3800);
        assertEquals("STRAIGHT_LINE", result.source());
    }

    private GeoPoint point(String latitude, String longitude) {
        return new GeoPoint(new BigDecimal(latitude), new BigDecimal(longitude));
    }
}
