package com.cameleer.server.app.controller;

import com.cameleer.server.app.AbstractPostgresIT;
import com.cameleer.server.app.TestSecurityHelper;
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.HttpHeaders;
import org.springframework.http.HttpMethod;

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

/**
 * Integration test for forward compatibility (API-05).
 * Verifies that unknown JSON fields in request bodies do not cause deserialization errors.
 */
class ForwardCompatIT extends AbstractPostgresIT {

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private TestSecurityHelper securityHelper;

    private String jwt;

    @BeforeEach
    void setUp() {
        jwt = securityHelper.registerTestAgent("test-agent-forward-compat-it");
    }

    @Test
    void unknownFieldsInRequestBodyDoNotCauseError() {
        String jsonWithUnknownFields = """
                {
                    "futureField": "value",
                    "anotherUnknown": 42
                }
                """;

        HttpHeaders headers = securityHelper.authHeaders(jwt);
        var entity = new HttpEntity<>(jsonWithUnknownFields, headers);

        var response = restTemplate.exchange(
                "/api/v1/data/executions", HttpMethod.POST, entity, String.class);

        assertThat(response.getStatusCode().value())
                .as("Unknown JSON fields must not cause 400 or 422 deserialization error")
                .isNotIn(400, 422);
    }
}
