using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FrameworkBLL.DrillDown;
using FrameworkDAL.CustomCode.DrillDown;
using FrameworkDAL.DTO.DrillDown;
using GB5Shared.DTO.Framework.CommonConfig;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.GenerateAutoNumber;
using GB5Shared.QueryExecutor;
using GB5Shared.Validation;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
namespace FrameworkTests;
///
/// Regression coverage for a real, pre-existing bug found while building Metadata Promotion's
/// DrillDown adapter: SaveDrillDown minted detail-row IDs by incrementing the PARENT's own
/// DrillDownId in a loop (`child.DrillDownDetailId = DrillDownDTO.DrillDownId++`), corrupting the
/// parent's own primary key by +N (N = detail row count) before it was even saved, and leaving
/// every child's own DrillDownId FK wrong. Fixed to mint detail IDs via a real AutoNumber
/// reservation (entity code "DRILLDOWNDETAIL") instead — this locks that fix in place.
///
/// AutoNumber.GetAutoNumber (SQL Server path) calls IQueryExecutor.ExecuteScalarAsync<long>
/// with an UPDATE...OUTPUT DELETED.AUTOID statement, returning previousAutoId directly (not a
/// post-incremented value) — StartNumber = previousAutoId + 1 + LoginDTO.ServerConfigOffset(0 here).
///
public class DrillDownBLLTests
{
private static LoginDTO Login(int userId = 7) => new() { ClientId = -1, UserId = userId };
private static AutoNumber RealAutoNumberReserving(Queue previousAutoIdsInCallOrder)
{
var qe = new Mock();
qe.Setup(x => x.ExecuteScalarAsync(
It.IsAny(), It.IsAny(), It.IsAny