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

import static io.restassured.RestAssured.given;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import dev.lions.unionflow.server.api.dto.mutuelle.epargne.TransactionEpargneResponse;
import dev.lions.unionflow.server.service.mutuelle.epargne.TransactionEpargneService;
import io.quarkus.test.InjectMock;
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;

/**
 * Tests mock pour TransactionEpargneResource — couvre la méthode transferer.
 */
@QuarkusTest
@DisplayName("TransactionEpargneResource (mock)")
class TransactionEpargneResourceMockTest {

    @InjectMock
    TransactionEpargneService transactionEpargneService;

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = {"ADMIN"})
    @DisplayName("POST /api/v1/epargne/transactions/transfert avec corps valide retourne 201")
    void transferer_valid_returns201() {
        TransactionEpargneResponse response = TransactionEpargneResponse.builder()
                .build();
        when(transactionEpargneService.transferer(any())).thenReturn(response);

        String body = String.format("""
                {
                  "compteId": "%s",
                  "typeTransaction": "TRANSFERT_SORTANT",
                  "montant": 5000,
                  "compteDestinationId": "%s",
                  "motif": "Transfert test"
                }
                """, UUID.randomUUID(), UUID.randomUUID());

        given()
                .contentType(ContentType.JSON)
                .body(body)
                .when()
                .post("/api/v1/epargne/transactions/transfert")
                .then()
                .statusCode(201);
    }

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

    @Test
    @DisplayName("POST /api/v1/epargne/transactions/transfert sans authentification retourne 401")
    void transferer_sansAuthentification_returns401() {
        String body = String.format("""
                {
                  "compteId": "%s",
                  "typeTransaction": "TRANSFERT_SORTANT",
                  "montant": 5000,
                  "compteDestinationId": "%s",
                  "motif": "Transfert test"
                }
                """, UUID.randomUUID(), UUID.randomUUID());

        given()
                .contentType(ContentType.JSON)
                .body(body)
                .when()
                .post("/api/v1/epargne/transactions/transfert")
                .then()
                .statusCode(401);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = {"ADMIN"})
    @DisplayName("POST /api/v1/epargne/transactions/transfert quand service lève exception retourne 500")
    void transferer_serviceException_returns500() {
        when(transactionEpargneService.transferer(any()))
                .thenThrow(new RuntimeException("Solde insuffisant pour le transfert"));

        String body = String.format("""
                {
                  "compteId": "%s",
                  "typeTransaction": "TRANSFERT_SORTANT",
                  "montant": 5000,
                  "compteDestinationId": "%s",
                  "motif": "Transfert erreur"
                }
                """, UUID.randomUUID(), UUID.randomUUID());

        given()
                .contentType(ContentType.JSON)
                .body(body)
                .when()
                .post("/api/v1/epargne/transactions/transfert")
                .then()
                .statusCode(500);
    }

    @Test
    @TestSecurity(user = "admin@unionflow.com", roles = {"ADMIN"})
    @DisplayName("POST /api/v1/epargne/transactions/transfert avec corps vide retourne 400")
    void transferer_corpsVide_returns400() {
        given()
                .contentType(ContentType.JSON)
                .body("{}")
                .when()
                .post("/api/v1/epargne/transactions/transfert")
                .then()
                .statusCode(400);
    }
}
