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.HttpStatus;
import org.springframework.http.ResponseEntity;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

class DiagramControllerIT extends AbstractPostgresIT {

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private TestSecurityHelper securityHelper;

    private HttpHeaders authHeaders;

    @BeforeEach
    void setUp() {
        String jwt = securityHelper.registerTestAgent("test-agent-diagram-it");
        authHeaders = securityHelper.authHeaders(jwt);
    }

    @Test
    void postSingleDiagram_returns202() {
        String json = """
                {
                  "routeId": "diagram-route-1",
                  "description": "Test route",
                  "version": 1,
                  "nodes": [],
                  "edges": []
                }
                """;

        ResponseEntity<String> response = restTemplate.postForEntity(
                "/api/v1/data/diagrams",
                new HttpEntity<>(json, authHeaders),
                String.class);

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

    @Test
    void postDiagram_dataAppearsAfterFlush() {
        String json = """
                {
                  "routeId": "diagram-flush-route",
                  "description": "Flush test",
                  "version": 1,
                  "nodes": [],
                  "edges": []
                }
                """;

        restTemplate.postForEntity(
                "/api/v1/data/diagrams",
                new HttpEntity<>(json, authHeaders),
                String.class);

        await().atMost(10, SECONDS).untilAsserted(() -> {
            Integer count = jdbcTemplate.queryForObject(
                    "SELECT count(*) FROM route_diagrams WHERE route_id = 'diagram-flush-route'",
                    Integer.class);
            assertThat(count).isGreaterThanOrEqualTo(1);
        });
    }

    @Test
    void postArrayOfDiagrams_returns202() {
        String json = """
                [{
                  "routeId": "diagram-arr-1",
                  "version": 1,
                  "nodes": [],
                  "edges": []
                },
                {
                  "routeId": "diagram-arr-2",
                  "version": 1,
                  "nodes": [],
                  "edges": []
                }]
                """;

        ResponseEntity<String> response = restTemplate.postForEntity(
                "/api/v1/data/diagrams",
                new HttpEntity<>(json, authHeaders),
                String.class);

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