package com.mosquito.project.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mosquito.project.dto.RegisterCallbackRequest;
import com.mosquito.project.dto.CreateActivityRequest;
import com.mosquito.project.service.ActivityService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = {
    "spring.flyway.enabled=false",
    "app.rate-limit.per-minute=2",
    "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration"
})
class CallbackControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private ActivityService activityService;

    private String anyValidApiKey() {
        CreateActivityRequest r = new CreateActivityRequest();
        r.setName("cb");
        r.setStartTime(java.time.ZonedDateTime.now());
        r.setEndTime(java.time.ZonedDateTime.now().plusDays(1));
        var act = activityService.createActivity(r);
        com.mosquito.project.dto.CreateApiKeyRequest k = new com.mosquito.project.dto.CreateApiKeyRequest();
        k.setActivityId(act.getId());
        k.setName("k");
        return activityService.generateApiKey(k);
    }

    @Test
    void shouldBeIdempotent_andRateLimited() throws Exception {
        String key = anyValidApiKey();
        RegisterCallbackRequest req = new RegisterCallbackRequest();
        req.setTrackingId("track-001");
        mockMvc.perform(post("/api/v1/callback/register")
                .header("X-API-Key", key)
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(req)))
            .andExpect(status().isOk());
        // 2nd same tracking id should still be OK (idempotent)
        mockMvc.perform(post("/api/v1/callback/register")
                .header("X-API-Key", key)
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(req)))
            .andExpect(status().isOk());

        // exceed rate limit (limit=2 per minute) with a different tracking id
        RegisterCallbackRequest req2 = new RegisterCallbackRequest();
        req2.setTrackingId("track-002");
        mockMvc.perform(post("/api/v1/callback/register")
                .header("X-API-Key", key)
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(req2)))
            .andExpect(status().isTooManyRequests());
    }
}

