using Microsoft.EntityFrameworkCore;
using TakeoutSaaS.Domain.Inventory.Entities;
using TakeoutSaaS.Domain.Inventory.Enums;
using TakeoutSaaS.Domain.Inventory.Repositories;
using TakeoutSaaS.Infrastructure.App.Persistence;

namespace TakeoutSaaS.Infrastructure.App.Repositories;

/// <summary>
/// 库存仓储 EF 实现。
/// </summary>
/// <remarks>
/// 提供库存与批次的读写能力。
/// </remarks>
public sealed class EfInventoryRepository(TakeoutAppDbContext context) : IInventoryRepository
{
    /// <inheritdoc />
    public Task<InventoryItem?> FindByIdAsync(long inventoryItemId, long tenantId, CancellationToken cancellationToken = default)
    {
        return context.InventoryItems
            .AsNoTracking()
            .Where(x => x.TenantId == tenantId && x.Id == inventoryItemId)
            .FirstOrDefaultAsync(cancellationToken);
    }

    /// <inheritdoc />
    public Task<InventoryItem?> FindBySkuAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
    {
        return context.InventoryItems
            .AsNoTracking()
            .Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
            .FirstOrDefaultAsync(cancellationToken);
    }

    /// <inheritdoc />
    public Task<InventoryItem?> GetForUpdateAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
    {
        return context.InventoryItems
            .Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
            .FirstOrDefaultAsync(cancellationToken);
    }

    /// <inheritdoc />
    public Task AddItemAsync(InventoryItem item, CancellationToken cancellationToken = default)
    {
        return context.InventoryItems.AddAsync(item, cancellationToken).AsTask();
    }

    /// <inheritdoc />
    public Task UpdateItemAsync(InventoryItem item, CancellationToken cancellationToken = default)
    {
        context.InventoryItems.Update(item);
        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public Task AddAdjustmentAsync(InventoryAdjustment adjustment, CancellationToken cancellationToken = default)
    {
        return context.InventoryAdjustments.AddAsync(adjustment, cancellationToken).AsTask();
    }

    /// <inheritdoc />
    public Task AddLockAsync(InventoryLockRecord lockRecord, CancellationToken cancellationToken = default)
    {
        return context.InventoryLockRecords.AddAsync(lockRecord, cancellationToken).AsTask();
    }

    /// <inheritdoc />
    public Task<InventoryLockRecord?> FindLockByKeyAsync(long tenantId, string idempotencyKey, CancellationToken cancellationToken = default)
    {
        return context.InventoryLockRecords
            .Where(x => x.TenantId == tenantId && x.IdempotencyKey == idempotencyKey)
            .FirstOrDefaultAsync(cancellationToken);
    }

    /// <inheritdoc />
    public Task MarkLockStatusAsync(InventoryLockRecord lockRecord, InventoryLockStatus status, CancellationToken cancellationToken = default)
    {
        lockRecord.Status = status;
        context.InventoryLockRecords.Update(lockRecord);
        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<InventoryLockRecord>> FindExpiredLocksAsync(long tenantId, DateTime utcNow, CancellationToken cancellationToken = default)
    {
        var locks = await context.InventoryLockRecords
            .Where(x => x.TenantId == tenantId && x.Status == InventoryLockStatus.Locked && x.ExpiresAt != null && x.ExpiresAt <= utcNow)
            .ToListAsync(cancellationToken);
        return locks;
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<InventoryBatch>> GetBatchesForConsumeAsync(long tenantId, long storeId, long productSkuId, InventoryBatchConsumeStrategy strategy, CancellationToken cancellationToken = default)
    {
        var query = context.InventoryBatches
            .Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId);

        query = strategy == InventoryBatchConsumeStrategy.Fefo
            ? query.OrderBy(x => x.ExpireDate ?? DateTime.MaxValue).ThenBy(x => x.BatchNumber)
            : query.OrderBy(x => x.BatchNumber);

        return await query.ToListAsync(cancellationToken);
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<InventoryBatch>> GetBatchesAsync(long tenantId, long storeId, long productSkuId, CancellationToken cancellationToken = default)
    {
        var batches = await context.InventoryBatches
            .AsNoTracking()
            .Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId)
            .OrderBy(x => x.ExpireDate ?? DateTime.MaxValue)
            .ThenBy(x => x.BatchNumber)
            .ToListAsync(cancellationToken);

        return batches;
    }

    /// <inheritdoc />
    public Task<InventoryBatch?> GetBatchForUpdateAsync(long tenantId, long storeId, long productSkuId, string batchNumber, CancellationToken cancellationToken = default)
    {
        return context.InventoryBatches
            .Where(x => x.TenantId == tenantId && x.StoreId == storeId && x.ProductSkuId == productSkuId && x.BatchNumber == batchNumber)
            .FirstOrDefaultAsync(cancellationToken);
    }

    /// <inheritdoc />
    public Task AddBatchAsync(InventoryBatch batch, CancellationToken cancellationToken = default)
    {
        return context.InventoryBatches.AddAsync(batch, cancellationToken).AsTask();
    }

    /// <inheritdoc />
    public Task UpdateBatchAsync(InventoryBatch batch, CancellationToken cancellationToken = default)
    {
        context.InventoryBatches.Update(batch);
        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public Task SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        return context.SaveChangesAsync(cancellationToken);
    }
}
