package dev.lions.unionflow.server.resource;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.security.TestSecurity;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@QuarkusTest
class PropositionAideResourceTest {

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/propositions-aide retourne 200")
    void listerToutes_returns200() {
        given()
                .queryParam("page", 0)
                .queryParam("size", 20)
                .when()
                .get("/api/propositions-aide")
                .then()
                .statusCode(200)
                .body("$", notNullValue());
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/propositions-aide/{id} inexistant retourne 404")
    void obtenirParId_inexistant_returns404() {
        given()
                .pathParam("id", "inexistant-id")
                .when()
                .get("/api/propositions-aide/{id}")
                .then()
                .statusCode(404);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/propositions-aide/meilleures retourne 200")
    void getMeilleures_returns200() {
        given()
                .when()
                .get("/api/propositions-aide/meilleures")
                .then()
                .statusCode(200)
                .body("$", notNullValue());
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/propositions-aide creer avec données valides retourne 201")
    void creer_valid_returns201() {
        String body = "{"
                + "\"typeAide\": \"AIDE_FRAIS_MEDICAUX\","
                + "\"titre\": \"Proposition test resource\","
                + "\"description\": \"Description de la proposition de test\","
                + "\"montantMaximum\": 50000,"
                + "\"nombreMaxBeneficiaires\": 5,"
                + "\"devise\": \"XOF\","
                + "\"proposantId\": \"proposant-test-id\","
                + "\"delaiReponseHeures\": 48"
                + "}";

        given()
                .contentType("application/json")
                .body(body)
                .when()
                .post("/api/propositions-aide")
                .then()
                .statusCode(anyOf(equalTo(201), equalTo(200), equalTo(400), equalTo(500)));
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("PUT /api/propositions-aide/{id} mettreAJour avec ID inexistant retourne 404 ou autre")
    void mettreAJour_inexistant_returns4xx() {
        String body = "{"
                + "\"typeAide\": \"AIDE_FRAIS_MEDICAUX\","
                + "\"titre\": \"Proposition mise a jour\","
                + "\"description\": \"Description mise a jour\""
                + "}";

        given()
                .contentType("application/json")
                .pathParam("id", "id-inexistant-test")
                .body(body)
                .when()
                .put("/api/propositions-aide/{id}")
                .then()
                .statusCode(anyOf(equalTo(404), equalTo(400), equalTo(500)));
    }
}
