package de.effigenix.application.production;

import de.effigenix.domain.production.*;
import de.effigenix.shared.common.Quantity;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.common.UnitOfMeasure;
import de.effigenix.shared.security.ActorId;
import de.effigenix.shared.security.AuthorizationPort;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
@DisplayName("GetRecipe Use Case")
class GetRecipeTest {

    @Mock private RecipeRepository recipeRepository;
    @Mock private AuthorizationPort authPort;

    private GetRecipe getRecipe;
    private ActorId performedBy;

    @BeforeEach
    void setUp() {
        getRecipe = new GetRecipe(recipeRepository, authPort);
        performedBy = ActorId.of("admin-user");
    }

    private Recipe activeRecipe(String id) {
        return Recipe.reconstitute(
                RecipeId.of(id), new RecipeName("Bratwurst"), 1, RecipeType.FINISHED_PRODUCT,
                "Beschreibung", new YieldPercentage(85), 14,
                Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
                "article-123", RecipeStatus.ACTIVE, List.of(), List.of(),
                OffsetDateTime.now(ZoneOffset.UTC), OffsetDateTime.now(ZoneOffset.UTC)
        );
    }

    @Test
    @DisplayName("should_ReturnRecipe_When_RecipeExists")
    void should_ReturnRecipe_When_RecipeExists() {
        var recipeId = RecipeId.of("recipe-1");
        var recipe = activeRecipe("recipe-1");
        when(authPort.can(performedBy, ProductionAction.RECIPE_READ)).thenReturn(true);
        when(recipeRepository.findById(recipeId)).thenReturn(Result.success(Optional.of(recipe)));

        var result = getRecipe.execute(recipeId, performedBy);

        assertThat(result.isSuccess()).isTrue();
        assertThat(result.unsafeGetValue().id()).isEqualTo(recipeId);
    }

    @Test
    @DisplayName("should_FailWithRecipeNotFound_When_RecipeDoesNotExist")
    void should_FailWithRecipeNotFound_When_RecipeDoesNotExist() {
        var recipeId = RecipeId.of("nonexistent");
        when(authPort.can(performedBy, ProductionAction.RECIPE_READ)).thenReturn(true);
        when(recipeRepository.findById(recipeId)).thenReturn(Result.success(Optional.empty()));

        var result = getRecipe.execute(recipeId, performedBy);

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(RecipeError.RecipeNotFound.class);
    }

    @Test
    @DisplayName("should_FailWithUnauthorized_When_ActorLacksPermission")
    void should_FailWithUnauthorized_When_ActorLacksPermission() {
        when(authPort.can(performedBy, ProductionAction.RECIPE_READ)).thenReturn(false);

        var result = getRecipe.execute(RecipeId.of("recipe-1"), performedBy);

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(RecipeError.Unauthorized.class);
        verify(recipeRepository, never()).findById(any());
    }
}
