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 io.restassured.http.ContentType;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@QuarkusTest
class PreferencesResourceTest {

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/preferences/{id} retourne 200")
    void obtenirPreferences_returns200() {
        given()
                .pathParam("utilisateurId", UUID.randomUUID())
                .when()
                .get("/api/preferences/{utilisateurId}")
                .then()
                .statusCode(200);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("PUT /api/preferences/{id} retourne 204")
    void mettreAJourPreferences_returns204() {
        given()
                .pathParam("utilisateurId", UUID.randomUUID())
                .contentType(ContentType.JSON)
                .body("{}")
                .when()
                .put("/api/preferences/{utilisateurId}")
                .then()
                .statusCode(204);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("POST /api/preferences/{id}/reinitialiser retourne 204")
    void reinitialiserPreferences_returns204() {
        given()
                .contentType("application/json")
                .pathParam("utilisateurId", UUID.randomUUID())
                .when()
                .post("/api/preferences/{utilisateurId}/reinitialiser")
                .then()
                .statusCode(204);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = { "ADMIN" })
    @DisplayName("GET /api/preferences/{id}/export retourne 200")
    void exporterPreferences_returns200() {
        given()
                .pathParam("utilisateurId", UUID.randomUUID())
                .when()
                .get("/api/preferences/{utilisateurId}/export")
                .then()
                .statusCode(200);
    }

    // -------------------------------------------------------------------------
    // Error cases
    // -------------------------------------------------------------------------

    @Test
    @DisplayName("GET /api/preferences/{id} sans authentification retourne 401")
    void obtenirPreferences_sansAuthentification_returns401() {
        given()
                .pathParam("utilisateurId", UUID.randomUUID())
                .when()
                .get("/api/preferences/{utilisateurId}")
                .then()
                .statusCode(401);
    }

    @Test
    @TestSecurity(user = "membre@unionflow.com", roles = { "MEMBRE" })
    @DisplayName("PUT /api/preferences/{id} avec rôle MEMBRE retourne 403")
    void mettreAJourPreferences_avecRoleMembre_returns403() {
        given()
                .pathParam("utilisateurId", UUID.randomUUID())
                .contentType(ContentType.JSON)
                .body("{}")
                .when()
                .put("/api/preferences/{utilisateurId}")
                .then()
                .statusCode(403);
    }

    @Test
    @DisplayName("PUT /api/preferences/{id} sans authentification retourne 401")
    void mettreAJourPreferences_sansAuthentification_returns401() {
        given()
                .pathParam("utilisateurId", UUID.randomUUID())
                .contentType(ContentType.JSON)
                .body("{}")
                .when()
                .put("/api/preferences/{utilisateurId}")
                .then()
                .statusCode(401);
    }
}
