using CMSMicroservice.Domain.Entities;
using CMSMicroservice.Domain.Enums;
namespace CMSMicroservice.Application.Common.Interfaces;
///
/// سرویس مدیریت موجودی - لایه بالاتر برای عملیات business
/// این سرویس مسئول همگامسازی موجودی بین InventoryItem و Product.RemainingCount است
///
public interface IInventoryService
{
#region Initialization
///
/// ایجاد رکورد موجودی برای محصول جدید
/// این متد باید در CreateProductCommandHandler و CreateDiscountProductCommandHandler فراخوانی شود
///
/// شناسه محصول (Product.Id یا DiscountProduct.Id)
/// نوع محصول (RegularProduct یا DiscountProduct)
/// موجودی اولیه
/// شناسه انبار (پیشفرض: انبار اصلی)
/// آستانه هشدار کمموجودی
/// CancellationToken
/// شناسه InventoryItem ایجاد شده
Task InitializeInventoryAsync(
long productId,
ProductType productType,
int initialQuantity,
long? warehouseId = null,
int lowStockThreshold = 10,
CancellationToken ct = default);
#endregion
#region Query Operations
///
/// دریافت موجودی یک محصول
///
Task GetInventoryAsync(
long productId,
ProductType productType,
long? warehouseId = null,
CancellationToken ct = default);
///
/// دریافت موجودی قابل فروش (Quantity - ReservedQuantity)
///
Task GetAvailableQuantityAsync(
long productId,
ProductType productType,
long? warehouseId = null,
CancellationToken ct = default);
///
/// بررسی اینکه آیا موجودی کافی برای فروش وجود دارد
///
Task CheckAvailabilityAsync(
long productId,
ProductType productType,
int requiredQuantity,
long? warehouseId = null,
CancellationToken ct = default);
///
/// دریافت لیست محصولات کمموجود
///
Task> GetLowStockItemsAsync(
ProductType? productType = null,
long? warehouseId = null,
int count = 50,
CancellationToken ct = default);
///
/// دریافت تاریخچه حرکات موجودی
///
Task> GetStockMovementsAsync(
long productId,
ProductType productType,
DateTime? fromDate = null,
DateTime? toDate = null,
CancellationToken ct = default);
#endregion
#region Order Flow Operations
///
/// رزرو موجودی برای سفارش pending
/// این متد در PlaceOrderCommandHandler فراخوانی میشود
/// فقط ReservedQuantity را افزایش میدهد، Quantity تغییر نمیکند
///
/// شناسه محصول
/// نوع محصول
/// تعداد رزرو
/// شناسه سفارش (Order.Id یا DiscountOrder.Id)
/// CancellationToken
/// true اگر رزرو موفق بود
Task ReserveStockAsync(
long productId,
ProductType productType,
int quantity,
long? orderId = null,
CancellationToken ct = default);
///
/// آزادسازی رزرو (لغو سفارش یا timeout)
/// این متد در CancelOrderCommandHandler فراخوانی میشود
///
Task ReleaseReservationAsync(
long productId,
ProductType productType,
int quantity,
long? orderId = null,
CancellationToken ct = default);
///
/// تایید فروش - کسر واقعی موجودی
/// این متد در CompleteOrderPaymentCommandHandler فراخوانی میشود
/// ReservedQuantity کاهش مییابد، Quantity کاهش مییابد، Product.RemainingCount sync میشود
///
Task ConfirmSaleAsync(
long productId,
ProductType productType,
int quantity,
long? orderId = null,
CancellationToken ct = default);
#endregion
#region Stock Management Operations
///
/// ورود کالا به انبار (Restock)
///
/// شناسه محصول
/// نوع محصول
/// تعداد ورودی
/// شماره مرجع (مثل شماره فاکتور خرید)
/// یادداشت
/// شناسه کاربر انجامدهنده
/// CancellationToken
Task AddStockAsync(
long productId,
ProductType productType,
int quantity,
string? referenceNumber = null,
string? note = null,
long? performedByUserId = null,
CancellationToken ct = default);
///
/// تعدیل موجودی (تنظیم به مقدار جدید)
///
Task AdjustStockAsync(
long productId,
ProductType productType,
int newQuantity,
string? note = null,
long? performedByUserId = null,
CancellationToken ct = default);
///
/// ثبت برگشت کالا از مشتری
///
Task ProcessReturnAsync(
long productId,
ProductType productType,
int quantity,
long? orderId = null,
string? note = null,
long? performedByUserId = null,
CancellationToken ct = default);
///
/// ثبت ضایعات/مفقودی
///
Task RecordLossAsync(
long productId,
ProductType productType,
int quantity,
StockMovementType lossType, // Damaged or Lost
string? note = null,
long? performedByUserId = null,
CancellationToken ct = default);
#endregion
#region Bulk Operations
///
/// رزرو موجودی برای چند آیتم (یک سفارش با چند محصول)
///
Task BulkReserveStockAsync(
IEnumerable<(long ProductId, ProductType ProductType, int Quantity)> items,
long? orderId = null,
CancellationToken ct = default);
///
/// آزادسازی رزرو برای چند آیتم
///
Task BulkReleaseReservationAsync(
IEnumerable<(long ProductId, ProductType ProductType, int Quantity)> items,
long? orderId = null,
CancellationToken ct = default);
///
/// تایید فروش برای چند آیتم
///
Task BulkConfirmSaleAsync(
IEnumerable<(long ProductId, ProductType ProductType, int Quantity)> items,
long? orderId = null,
CancellationToken ct = default);
#endregion
}