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

namespace TakeoutSaaS.Infrastructure.Dictionary.Repositories;

/// <summary>
/// 字典标签覆盖仓储实现。
/// </summary>
public sealed class DictionaryLabelOverrideRepository(DictionaryDbContext context) : IDictionaryLabelOverrideRepository
{
    /// <summary>
    /// 根据 ID 获取覆盖配置。
    /// </summary>
    public Task<DictionaryLabelOverride?> GetByIdAsync(long id, CancellationToken cancellationToken = default)
    {
        return context.DictionaryLabelOverrides
            .Include(x => x.DictionaryItem)
            .FirstOrDefaultAsync(x => x.Id == id && x.DeletedAt == null, cancellationToken);
    }

    /// <summary>
    /// 获取指定字典项的覆盖配置。
    /// </summary>
    public Task<DictionaryLabelOverride?> GetByItemIdAsync(long tenantId, long dictionaryItemId, CancellationToken cancellationToken = default)
    {
        return context.DictionaryLabelOverrides
            .FirstOrDefaultAsync(x =>
                x.TenantId == tenantId &&
                x.DictionaryItemId == dictionaryItemId &&
                x.DeletedAt == null,
                cancellationToken);
    }

    /// <summary>
    /// 获取租户的所有覆盖配置。
    /// </summary>
    public async Task<IReadOnlyList<DictionaryLabelOverride>> ListByTenantAsync(
        long tenantId,
        OverrideType? overrideType = null,
        CancellationToken cancellationToken = default)
    {
        var query = context.DictionaryLabelOverrides
            .AsNoTracking()
            .Include(x => x.DictionaryItem)
            .Where(x => x.TenantId == tenantId && x.DeletedAt == null);

        if (overrideType.HasValue)
        {
            query = query.Where(x => x.OverrideType == overrideType.Value);
        }

        return await query.OrderByDescending(x => x.CreatedAt).ToListAsync(cancellationToken);
    }

    /// <summary>
    /// 批量获取多个字典项的覆盖配置。
    /// </summary>
    public async Task<IReadOnlyList<DictionaryLabelOverride>> GetByItemIdsAsync(
        long tenantId,
        IEnumerable<long> dictionaryItemIds,
        CancellationToken cancellationToken = default)
    {
        var ids = dictionaryItemIds.ToArray();
        if (ids.Length == 0) return Array.Empty<DictionaryLabelOverride>();

        return await context.DictionaryLabelOverrides
            .AsNoTracking()
            .Where(x =>
                x.TenantId == tenantId &&
                ids.Contains(x.DictionaryItemId) &&
                x.DeletedAt == null)
            .ToListAsync(cancellationToken);
    }

    /// <summary>
    /// 新增覆盖配置。
    /// </summary>
    public Task AddAsync(DictionaryLabelOverride entity, CancellationToken cancellationToken = default)
    {
        context.DictionaryLabelOverrides.Add(entity);
        return Task.CompletedTask;
    }

    /// <summary>
    /// 更新覆盖配置。
    /// </summary>
    public Task UpdateAsync(DictionaryLabelOverride entity, CancellationToken cancellationToken = default)
    {
        context.DictionaryLabelOverrides.Update(entity);
        return Task.CompletedTask;
    }

    /// <summary>
    /// 删除覆盖配置。
    /// </summary>
    public Task DeleteAsync(DictionaryLabelOverride entity, CancellationToken cancellationToken = default)
    {
        entity.DeletedAt = DateTime.UtcNow;
        context.DictionaryLabelOverrides.Update(entity);
        return Task.CompletedTask;
    }

    /// <summary>
    /// 持久化更改。
    /// </summary>
    public Task SaveChangesAsync(CancellationToken cancellationToken = default)
        => context.SaveChangesAsync(cancellationToken);
}
