package ua.tqs;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import ua.tqs.controller.MunincipalityController;
import ua.tqs.service.MunincipalityService;

import java.util.List;
import java.util.Map;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(MunincipalityController.class)
@AutoConfigureMockMvc
class MunincipalityControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private MunincipalityService service;

    @Test
    void testGetAllMunicipalities() throws Exception {
        when(service.getAllMunicipalities()).thenReturn(List.of(
                "Aguiar da Beira",
                "Vila Nova de Gaia"
        ));

        mockMvc.perform(get("/api/munincipalities")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0]").value("Aguiar da Beira"))
                .andExpect(jsonPath("$[1]").value("Vila Nova de Gaia"));
    }

    @Disabled("API rate limit")
    @Test
    void testGetByDistrict() throws Exception {
        when(service.getMunicipalitiesByDistrict("Aveiro")).thenReturn(List.of(
                Map.of("nome", "Aveiro")
        ));

        mockMvc.perform(get("/api/munincipalities/Aveiro")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0]").value("Aveiro"));
    }
}