package dev.lions.unionflow.server.resource;

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

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

@QuarkusTest
class AuditResourceTest {

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

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/audit avec sortBy et sortOrder retourne 200")
    void listerTous_withSortParams_returns200() {
        given()
                .queryParam("page", 0)
                .queryParam("size", 10)
                .queryParam("sortBy", "dateHeure")
                .queryParam("sortOrder", "asc")
                .when()
                .get("/api/audit")
                .then()
                .statusCode(200)
                .body("data", notNullValue())
                .body("total", notNullValue());
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/audit/rechercher retourne 200")
    void rechercher_returns200() {
        given()
                .contentType(ContentType.JSON)
                .when()
                .post("/api/audit/rechercher")
                .then()
                .statusCode(200)
                .body("$", notNullValue());
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/audit/rechercher avec filtres retourne 200")
    void rechercher_withFilters_returns200() {
        given()
                .contentType(ContentType.JSON)
                .queryParam("typeAction", "LOGIN")
                .queryParam("severite", "INFO")
                .queryParam("module", "AUTH")
                .queryParam("page", 0)
                .queryParam("size", 10)
                .when()
                .post("/api/audit/rechercher")
                .then()
                .statusCode(anyOf(equalTo(200), equalTo(500)));
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/audit enregistre un log et retourne 201")
    void enregistrerLog_returns201() {
        String body = """
                {
                    "typeAction": "TEST_RESOURCE",
                    "severite": "INFO",
                    "utilisateur": "admin@test.com",
                    "module": "TEST",
                    "description": "Test depuis AuditResourceTest"
                }
                """;
        given()
                .contentType(ContentType.JSON)
                .body(body)
                .when()
                .post("/api/audit")
                .then()
                .statusCode(anyOf(equalTo(201), equalTo(500)));
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/audit/statistiques retourne 200 avec les clés attendues")
    void getStatistiques_returns200() {
        given()
                .when()
                .get("/api/audit/statistiques")
                .then()
                .statusCode(200)
                .body("total", notNullValue())
                .body("success", notNullValue())
                .body("errors", notNullValue())
                .body("warnings", notNullValue());
    }
}
