using CashLineERP.Application.Inventory;
using CashLineERP.Contracts.Inventory;
using CashLineERP.Domain.Inventory;
using CashLineERP.Infrastructure.Persistence.Repositories;

namespace CashLineERP.Api.Tests;

public sealed class ReplenishmentOperationalHandoffServiceQaTests
{
    [Fact]
    public void Reorder_Plan_Handoff_Can_Create_Draft_Transfer_And_Procurement_Documents()
    {
        using var dbContext = QaDbContextFactory.CreateContext();

        var service = new ReplenishmentRuleService(new ReplenishmentRuleRepository(dbContext));

        dbContext.InventoryMovements.AddRange(
            new InventoryMovement(
                movementNumber: "IM-HANDOFF-1001-TGT",
                movementType: "GRN_RECEIPT",
                branchId: 1,
                itemId: 1001,
                fromWarehouseId: null,
                fromWarehouseLocationId: null,
                toWarehouseId: 2,
                toWarehouseLocationId: null,
                batchNumber: "HANDOFF-1001-TGT",
                serialNumber: null,
                stockStatus: "AVAILABLE",
                quantityMoved: 2m,
                unitCost: 24m,
                movedAtUtc: new DateTime(2026, 5, 20, 9, 0, 0, DateTimeKind.Utc)),
            new InventoryMovement(
                movementNumber: "IM-HANDOFF-1002-TGT",
                movementType: "GRN_RECEIPT",
                branchId: 1,
                itemId: 1002,
                fromWarehouseId: null,
                fromWarehouseLocationId: null,
                toWarehouseId: 2,
                toWarehouseLocationId: null,
                batchNumber: "HANDOFF-1002-TGT",
                serialNumber: null,
                stockStatus: "AVAILABLE",
                quantityMoved: 1m,
                unitCost: 40m,
                movedAtUtc: new DateTime(2026, 5, 20, 10, 0, 0, DateTimeKind.Utc)));
        dbContext.SaveChanges();

        var transferRule = service.ActivateReplenishmentRule(
            service.ApproveReplenishmentRule(
                service.SubmitReplenishmentRule(
                    service.CreateReplenishmentRule(new CreateReplenishmentRuleRequest(
                        RuleName: "Transfer handoff rule",
                        BranchId: 1,
                        TargetWarehouseId: 2,
                        PreferredSourceWarehouseId: 1,
                        ItemId: 1001,
                        UnitOfMeasureCode: "EA",
                        ReorderPointQuantity: 10m,
                        TargetSafetyStockQuantity: 12m,
                        TargetStockQuantity: 20m,
                        SourceSafetyStockQuantity: 15m,
                        Priority: 100,
                        EffectiveFromDate: new DateOnly(2026, 5, 1),
                        EffectiveToDate: new DateOnly(2026, 5, 31),
                        Notes: "Transfer handoff")).ReplenishmentRuleId,
                    2).ReplenishmentRuleId,
                4).ReplenishmentRuleId,
            4);

        var procurementRule = service.ActivateReplenishmentRule(
            service.ApproveReplenishmentRule(
                service.SubmitReplenishmentRule(
                    service.CreateReplenishmentRule(new CreateReplenishmentRuleRequest(
                        RuleName: "Procurement handoff rule",
                        BranchId: 1,
                        TargetWarehouseId: 2,
                        PreferredSourceWarehouseId: 1,
                        ItemId: 1002,
                        UnitOfMeasureCode: "BOX",
                        ReorderPointQuantity: 5m,
                        TargetSafetyStockQuantity: 6m,
                        TargetStockQuantity: 12m,
                        SourceSafetyStockQuantity: 100m,
                        Priority: 101,
                        EffectiveFromDate: new DateOnly(2026, 5, 1),
                        EffectiveToDate: new DateOnly(2026, 5, 31),
                        Notes: "Procurement handoff")).ReplenishmentRuleId,
                    2).ReplenishmentRuleId,
                4).ReplenishmentRuleId,
            4);

        var handoff = service.CreateOperationalHandoff(5, new ReorderPlanningRequest(
            BranchId: 1,
            AsOfDate: new DateOnly(2026, 5, 21),
            TargetWarehouseId: 2,
            ItemId: null));

        var transferDraft = Assert.Single(handoff.TransferDrafts);
        Assert.Equal(1, transferDraft.LineCount);
        Assert.Contains(transferRule.ReplenishmentRuleId, transferDraft.SourceReplenishmentRuleIds);

        var procurementDraft = Assert.Single(handoff.ProcurementDrafts);
        Assert.Equal(5L, procurementDraft.RequesterUserId);
        Assert.Contains(procurementRule.ReplenishmentRuleId, procurementDraft.SourceReplenishmentRuleIds);
        Assert.True(procurementDraft.LineCount >= 1);
    }
}
