package dev.lions.unionflow.server.resource.mutuelle;

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

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.security.TestSecurity;
import java.math.BigDecimal;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@QuarkusTest
class DemandeCreditResourceTest {

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/v1/mutuelle/credits/{id} inexistant retourne 404")
    void getDemandeById_inexistant_returns404() {
        given()
                .pathParam("id", UUID.randomUUID())
                .when()
                .get("/api/v1/mutuelle/credits/{id}")
                .then()
                .statusCode(404);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/v1/mutuelle/credits/membre/{id} retourne 200")
    void getDemandesByMembre_returns200() {
        given()
                .pathParam("membreId", UUID.randomUUID())
                .when()
                .get("/api/v1/mutuelle/credits/membre/{membreId}")
                .then()
                .statusCode(200)
                .body("$", notNullValue());
    }

    @Test
    @TestSecurity(user = "membre@unionflow.com", roles = { "MEMBRE" })
    @DisplayName("POST /api/v1/mutuelle/credits sans membre existant retourne 400 ou 404 ou 500")
    void soumettreDemande_membreInexistant_returnsError() {
        String body = "{\"membreId\":\"" + UUID.randomUUID() + "\",\"montantDemande\":100000,\"dureeMoisDemande\":12,\"motif\":\"Test\"}";
        given()
                .body(body)
                .contentType("application/json")
                .when()
                .post("/api/v1/mutuelle/credits")
                .then()
                .statusCode(anyOf(equalTo(400), equalTo(404), equalTo(500)));
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("PATCH /api/v1/mutuelle/credits/{id}/statut sans statut retourne 400")
    void changerStatut_sansStatut_returns400() {
        given()
                .pathParam("id", UUID.randomUUID())
                .when()
                .patch("/api/v1/mutuelle/credits/{id}/statut")
                .then()
                .statusCode(400);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("PATCH /api/v1/mutuelle/credits/{id}/statut avec statut sur id inexistant retourne 404")
    void changerStatut_idInexistant_returns404() {
        given()
                .pathParam("id", UUID.randomUUID())
                .queryParam("statut", "REJETEE")
                .when()
                .patch("/api/v1/mutuelle/credits/{id}/statut")
                .then()
                .statusCode(404);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/v1/mutuelle/credits/{id}/approbation sur id inexistant retourne 404 ou 415")
    void approuver_idInexistant_returns404() {
        given()
                .pathParam("id", UUID.randomUUID())
                .queryParam("montant", 100000)
                .queryParam("duree", 12)
                .queryParam("taux", 10)
                .contentType("application/json")
                .when()
                .post("/api/v1/mutuelle/credits/{id}/approbation")
                .then()
                .statusCode(anyOf(equalTo(404), equalTo(415)));
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/v1/mutuelle/credits/{id}/decaissement sur id inexistant retourne 404 ou 415 ou 500")
    void decaisser_idInexistant_returns404() {
        given()
                .pathParam("id", UUID.randomUUID())
                .queryParam("datePremiereEcheance", "2025-04-01")
                .contentType("application/json")
                .when()
                .post("/api/v1/mutuelle/credits/{id}/decaissement")
                .then()
                .statusCode(anyOf(equalTo(404), equalTo(415), equalTo(500)));
    }
}
