package io.ljunggren.usps.api.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.jupiter.api.Test;

import io.ljunggren.usps.api.authorization.AuthToken;
import io.ljunggren.usps.api.model.Debug;

public class OauthServiceTest {

    private final OauthService oauthService = new OauthService("https://example.com");

    @Test
    void authorizeReturnsToken() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse response = HttpTestUtils.mockResponse(200,
                "{\"access_token\":\"abc123\",\"token_type\":\"Bearer\",\"expires_in\":3600}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        AuthToken token = oauthService.authorize("clientId", "clientSecret", httpClient);

        assertEquals("abc123", token.getAccessToken());
        assertEquals("Bearer", token.getTokenType());
        assertEquals(3600, token.getExpiresIn());
        verify(httpClient).close();
    }

    @Test
    void authorizeThrowsOnThrottling() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse response = HttpTestUtils.mockResponse(429, "");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        assertThrows(Exception.class, () -> oauthService.authorize("clientId", "clientSecret", httpClient));
        verify(httpClient).close();
    }

    @Test
    void authorizeThrowsOnServiceUnavailable() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse response = HttpTestUtils.mockResponse(503, "");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        Exception ex = assertThrows(Exception.class,
                () -> oauthService.authorize("clientId", "clientSecret", httpClient));
        assertEquals("USPS API not found", ex.getMessage());
    }

    @Test
    void authorizeThrowsOnUnexpectedErrorStatus() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse response = HttpTestUtils.mockResponse(400, "{\"error\":\"invalid_client\"}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        Exception ex = assertThrows(Exception.class,
                () -> oauthService.authorize("clientId", "clientSecret", httpClient));
        assertEquals(true, ex.getMessage().contains("400"));
    }

    @Test
    void debugRedactsAccessToken() throws Exception {
        CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
        CloseableHttpResponse response = HttpTestUtils.mockResponse(200,
                "{\"access_token\":\"super-secret\",\"token_type\":\"Bearer\",\"expires_in\":3600}");
        when(httpClient.execute(any(HttpUriRequest.class))).thenReturn(response);

        Debug debug = oauthService.debug("clientId", "clientSecret", httpClient);

        assertFalse(debug.getResponse().contains("super-secret"));
        assertEquals(true, debug.getResponse().contains("REDACTED"));
    }

}
