package de.effigenix.application.inventory;

import de.effigenix.application.inventory.command.BookProductionConsumptionCommand;
import de.effigenix.domain.inventory.*;
import de.effigenix.domain.masterdata.article.ArticleId;
import de.effigenix.shared.common.Quantity;
import de.effigenix.shared.common.RepositoryError;
import de.effigenix.shared.common.Result;
import de.effigenix.shared.common.UnitOfMeasure;
import de.effigenix.shared.persistence.UnitOfWork;
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.Instant;
import java.time.LocalDate;
import java.util.List;
import java.util.function.Supplier;

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

@ExtendWith(MockitoExtension.class)
@DisplayName("BookProductionConsumption Use Case")
class BookProductionConsumptionTest {

    @Mock private StockRepository stockRepository;
    @Mock private StockMovementRepository stockMovementRepository;
    @Mock private UnitOfWork unitOfWork;

    private BookProductionConsumption bookProductionConsumption;

    @BeforeEach
    void setUp() {
        bookProductionConsumption = new BookProductionConsumption(stockRepository, stockMovementRepository, unitOfWork);
        lenient().when(unitOfWork.executeAtomically(any())).thenAnswer(inv -> ((Supplier<?>) inv.getArgument(0)).get());
    }

    private Stock stockWithBatch(String articleId, String batchReferenceId, String quantity) {
        var stockBatch = StockBatch.reconstitute(
                StockBatchId.of("sb-1"),
                new BatchReference(batchReferenceId, BatchType.PRODUCED),
                Quantity.of(new BigDecimal(quantity), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
                LocalDate.of(2026, 6, 1),
                StockBatchStatus.AVAILABLE,
                Instant.now()
        );
        return Stock.reconstitute(
                StockId.of("stock-1"),
                ArticleId.of(articleId),
                StorageLocationId.of("loc-prod"),
                null, null,
                List.of(stockBatch), List.of()
        );
    }

    private Stock stockWithoutMatchingBatch(String articleId) {
        var stockBatch = StockBatch.reconstitute(
                StockBatchId.of("sb-other"),
                new BatchReference("OTHER-BATCH", BatchType.PRODUCED),
                Quantity.of(new BigDecimal("100"), UnitOfMeasure.KILOGRAM).unsafeGetValue(),
                LocalDate.of(2026, 6, 1),
                StockBatchStatus.AVAILABLE,
                Instant.now()
        );
        return Stock.reconstitute(
                StockId.of("stock-2"),
                ArticleId.of(articleId),
                StorageLocationId.of("loc-prod"),
                null, null,
                List.of(stockBatch), List.of()
        );
    }

    private BookProductionConsumptionCommand validCommand() {
        return new BookProductionConsumptionCommand(
                "P-2026-03-19-001", "article-42", "5", "KILOGRAM");
    }

    @Test
    @DisplayName("should deduct stock and record movement when batch found")
    void should_DeductStock_When_BatchFound() {
        var stock = stockWithBatch("article-42", "P-2026-03-19-001", "50");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));
        when(stockRepository.save(any())).thenReturn(Result.success(null));
        when(stockMovementRepository.save(any())).thenReturn(Result.success(null));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isSuccess()).isTrue();
        verify(stockRepository).save(stock);
        verify(stockMovementRepository).save(any(StockMovement.class));
    }

    @Test
    @DisplayName("should fail when no stock exists for article")
    void should_Fail_When_NoStockForArticle() {
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of()));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.StockNotFound.class);
        verify(stockRepository, never()).save(any());
    }

    @Test
    @DisplayName("should fail when batch not found in any stock")
    void should_Fail_When_BatchNotInStock() {
        var stock = stockWithoutMatchingBatch("article-42");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.BatchNotFoundInStock.class);
        verify(stockRepository, never()).save(any());
    }

    @Test
    @DisplayName("should fail when removal quantity exceeds stock")
    void should_Fail_When_QuantityExceedsStock() {
        var stock = stockWithBatch("article-42", "P-2026-03-19-001", "3");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.RemovalFailed.class);
        verify(stockMovementRepository, never()).save(any());
    }

    @Test
    @DisplayName("should fail with RepositoryFailure when stock lookup fails")
    void should_Fail_When_StockLookupFails() {
        when(stockRepository.findAllByArticleId(any()))
                .thenReturn(Result.failure(new RepositoryError.DatabaseError("connection lost")));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.RepositoryFailure.class);
    }

    @Test
    @DisplayName("should fail when quantity amount is zero")
    void should_Fail_When_ZeroQuantity() {
        var stock = stockWithBatch("article-42", "P-2026-03-19-001", "50");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));

        var cmd = new BookProductionConsumptionCommand(
                "P-2026-03-19-001", "article-42", "0", "KILOGRAM");

        var result = bookProductionConsumption.execute(cmd);

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.RemovalFailed.class);
    }

    @Test
    @DisplayName("should fail when quantity unit is invalid")
    void should_Fail_When_InvalidQuantityUnit() {
        var stock = stockWithBatch("article-42", "P-2026-03-19-001", "50");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));

        var cmd = new BookProductionConsumptionCommand(
                "P-2026-03-19-001", "article-42", "5", "INVALID_UNIT");

        var result = bookProductionConsumption.execute(cmd);

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.RemovalFailed.class);
    }

    @Test
    @DisplayName("should fail with RepositoryFailure when movement save fails")
    void should_Fail_When_MovementSaveFails() {
        var stock = stockWithBatch("article-42", "P-2026-03-19-001", "50");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));
        when(stockRepository.save(any())).thenReturn(Result.success(null));
        when(stockMovementRepository.save(any()))
                .thenReturn(Result.failure(new RepositoryError.DatabaseError("write error")));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.RepositoryFailure.class);
    }

    @Test
    @DisplayName("should fail with RepositoryFailure when stock save fails")
    void should_Fail_When_StockSaveFails() {
        var stock = stockWithBatch("article-42", "P-2026-03-19-001", "50");
        when(stockRepository.findAllByArticleId(ArticleId.of("article-42")))
                .thenReturn(Result.success(List.of(stock)));
        when(stockRepository.save(any()))
                .thenReturn(Result.failure(new RepositoryError.DatabaseError("write error")));

        var result = bookProductionConsumption.execute(validCommand());

        assertThat(result.isFailure()).isTrue();
        assertThat(result.unsafeGetError()).isInstanceOf(ProductionConsumptionError.RepositoryFailure.class);
    }
}
