using CashLineERP.Application.Inventory;
using CashLineERP.SharedKernel.Workflow;
using Microsoft.EntityFrameworkCore;

namespace CashLineERP.Infrastructure.Persistence.Repositories;

public sealed class InventoryMovementReadRepository : IInventoryMovementReadRepository
{
    private readonly CashLineErpDbContext _dbContext;

    public InventoryMovementReadRepository(CashLineErpDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IReadOnlyCollection<InventoryMovementView> GetInventoryMovements() =>
        _dbContext.InventoryMovements
            .AsNoTracking()
            .OrderByDescending(movement => movement.MovedAtUtc)
            .ThenByDescending(movement => movement.Id)
            .Select(movement => new InventoryMovementView(
                movement.Id,
                movement.MovementNumber,
                movement.MovementType,
                movement.BranchId,
                movement.ItemId,
                movement.FromWarehouseId,
                movement.FromWarehouseLocationId,
                movement.ToWarehouseId,
                movement.ToWarehouseLocationId,
                movement.StockStatus,
                movement.BatchNumber,
                movement.SerialNumber,
                movement.QuantityMoved,
                movement.UnitCost,
                movement.MovedAtUtc,
                WorkflowStatusSeedIds.ToCode(movement.StatusId)))
            .ToArray();
}
