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

namespace TakeoutSaaS.Infrastructure.App.Repositories;

/// <summary>
/// 租户只读仓储实现（AdminApi 使用）。
/// </summary>
public sealed class EfTenantRepository(TakeoutAdminDbContext context) : ITenantRepository
{
    /// <inheritdoc />
    public Task<Tenant?> FindByIdAsync(long tenantId, CancellationToken cancellationToken = default)
    {
        // 1. 只读查询租户（跨租户）
        return context.Tenants
            .AsNoTracking()
            .FirstOrDefaultAsync(x => x.Id == tenantId, cancellationToken);
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<Tenant>> FindByIdsAsync(IReadOnlyCollection<long> tenantIds, CancellationToken cancellationToken = default)
    {
        // 1. tenantIds 为空直接返回
        if (tenantIds.Count == 0)
        {
            return Array.Empty<Tenant>();
        }

        // 2. (空行后) 批量查询租户
        return await context.Tenants
            .AsNoTracking()
            .Where(x => tenantIds.Contains(x.Id))
            .ToListAsync(cancellationToken);
    }
}
