package com.cameleer.server.app.controller;

import com.cameleer.server.app.AbstractPostgresIT;
import com.cameleer.server.app.TestSecurityHelper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

class ThresholdAdminControllerIT extends AbstractPostgresIT {

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private TestSecurityHelper securityHelper;

    private String adminJwt;
    private String viewerJwt;

    @BeforeEach
    void setUp() {
        adminJwt = securityHelper.adminToken();
        viewerJwt = securityHelper.viewerToken();
        jdbcTemplate.update("DELETE FROM server_config WHERE config_key = 'thresholds'");
    }

    @Test
    void getThresholds_asAdmin_returnsDefaults() throws Exception {
        ResponseEntity<String> response = restTemplate.exchange(
                "/api/v1/admin/thresholds", HttpMethod.GET,
                new HttpEntity<>(securityHelper.authHeadersNoBody(adminJwt)),
                String.class);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        JsonNode body = objectMapper.readTree(response.getBody());
        assertThat(body.has("database")).isTrue();
        assertThat(body.path("database").path("connectionPoolWarning").asInt()).isEqualTo(80);
    }

    @Test
    void getThresholds_asViewer_returns403() {
        ResponseEntity<String> response = restTemplate.exchange(
                "/api/v1/admin/thresholds", HttpMethod.GET,
                new HttpEntity<>(securityHelper.authHeadersNoBody(viewerJwt)),
                String.class);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
    }

    @Test
    void updateThresholds_asAdmin_returns200() throws Exception {
        String json = """
                {
                  "database": {
                    "connectionPoolWarning": 70,
                    "connectionPoolCritical": 90,
                    "queryDurationWarning": 2.0,
                    "queryDurationCritical": 15.0
                  }
                }
                """;

        ResponseEntity<String> response = restTemplate.exchange(
                "/api/v1/admin/thresholds", HttpMethod.PUT,
                new HttpEntity<>(json, securityHelper.authHeaders(adminJwt)),
                String.class);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        JsonNode body = objectMapper.readTree(response.getBody());
        assertThat(body.path("database").path("connectionPoolWarning").asInt()).isEqualTo(70);
    }

    @Test
    void updateThresholds_invalidWarningGreaterThanCritical_returns400() {
        String json = """
                {
                  "database": {
                    "connectionPoolWarning": 95,
                    "connectionPoolCritical": 80,
                    "queryDurationWarning": 2.0,
                    "queryDurationCritical": 15.0
                  }
                }
                """;

        ResponseEntity<String> response = restTemplate.exchange(
                "/api/v1/admin/thresholds", HttpMethod.PUT,
                new HttpEntity<>(json, securityHelper.authHeaders(adminJwt)),
                String.class);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
    }
}
