using System;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using TakeoutSaaS.Domain.Inventory.Entities;
using TakeoutSaaS.Domain.Inventory.Enums;

namespace TakeoutSaaS.Domain.Inventory.Repositories;

/// <summary>
/// 库存仓储契约。
/// </summary>
public interface IInventoryRepository
{
    /// <summary>
    /// 依据标识查询库存。
    /// </summary>
    Task<InventoryItem?> FindByIdAsync(long inventoryItemId, long tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 按门店与 SKU 查询库存（只读）。
    /// </summary>
    Task<InventoryItem?> FindBySkuAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 按门店与 SKU 查询库存（跟踪用于更新）。
    /// </summary>
    Task<InventoryItem?> GetForUpdateAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增库存记录。
    /// </summary>
    Task AddItemAsync(InventoryItem item, CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新库存记录。
    /// </summary>
    Task UpdateItemAsync(InventoryItem item, CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增库存调整记录。
    /// </summary>
    Task AddAdjustmentAsync(InventoryAdjustment adjustment, CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增锁定记录。
    /// </summary>
    Task AddLockAsync(InventoryLockRecord lockRecord, CancellationToken cancellationToken = default);

    /// <summary>
    /// 按幂等键查询锁记录。
    /// </summary>
    Task<InventoryLockRecord?> FindLockByKeyAsync(long tenantId, string idempotencyKey, CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新锁状态。
    /// </summary>
    Task MarkLockStatusAsync(InventoryLockRecord lockRecord, InventoryLockStatus status, CancellationToken cancellationToken = default);

    /// <summary>
    /// 查询过期锁定。
    /// </summary>
    Task<IReadOnlyList<InventoryLockRecord>> FindExpiredLocksAsync(long tenantId, DateTime utcNow, CancellationToken cancellationToken = default);

    /// <summary>
    /// 查询批次列表。
    /// </summary>
    Task<IReadOnlyList<InventoryBatch>> GetBatchesAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 批次扣减读取（带排序策略）。
    /// </summary>
    Task<IReadOnlyList<InventoryBatch>> GetBatchesForConsumeAsync(long tenantId, long storeId, long productSkuId, InventoryBatchConsumeStrategy strategy, CancellationToken cancellationToken = default);

    /// <summary>
    /// 查询批次（跟踪用于更新）。
    /// </summary>
    Task<InventoryBatch?> GetBatchForUpdateAsync(long tenantId, long storeId, long productSkuId, string batchNumber, CancellationToken cancellationToken = default);

    /// <summary>
    /// 新增批次。
    /// </summary>
    Task AddBatchAsync(InventoryBatch batch, CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新批次。
    /// </summary>
    Task UpdateBatchAsync(InventoryBatch batch, CancellationToken cancellationToken = default);

    /// <summary>
    /// 持久化变更。
    /// </summary>
    Task SaveChangesAsync(CancellationToken cancellationToken = default);
}
