using EnablementDAL.CustomCode.ShowRun; using EnablementDAL.DTO; using EnablementDAL.Query.Show; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; namespace EnablementDAL.Implementations; public class ShowRunDAL : IShowRunDAL { private readonly IQueryExecutor _qe; public ShowRunDAL(IQueryExecutor qe) => _qe = qe; // ------------------------------------------------------------------------- // Insert a new run record when a show is triggered. // The DTO must have TenantId populated by the caller (from login.ClientId). // Parameters come from ShowRunDTO properties — ensure DTO is fully mapped. // ------------------------------------------------------------------------- public async Task InsertShowRunAsync(ShowRunDTO dto, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, ShowRunQB.INSERT_SHOW_RUN, dto, cancellationToken: ct).ConfigureAwait(false); return dto.ShowRunId; } // ------------------------------------------------------------------------- // Advance the current step pointer as the user navigates forward/backward. // FIX: CurrentStepId → CurrentShowStepId (actual DDL column) // DatabaseName → TenantId (DatabaseName doesn't exist) // Parameters: @ShowRunId, @CurrentShowStepId, @ModifiedById, @TenantId, @UserId // ------------------------------------------------------------------------- public async Task UpdateCurrentStepAsync( int showRunId, int showStepId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, ShowRunQB.UPDATE_SHOW_RUN_STEP_ID, new { ShowRunId = showRunId, CurrentShowStepId = showStepId, ModifiedById = login.UserId, TenantId = login.ClientId, UserId = login.UserId }, cancellationToken: ct).ConfigureAwait(false); // ------------------------------------------------------------------------- // Mark a run as Completed / Dismissed / Expired / Failed. // FIX: DatabaseName → TenantId // Parameters: @ShowRunId, @RunState, @ModifiedById, @TenantId, @UserId // ------------------------------------------------------------------------- public async Task UpdateRunStateAsync( int showRunId, byte runState, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, ShowRunQB.UPDATE_SHOW_RUN_STATE, new { ShowRunId = showRunId, RunState = runState, ModifiedById = login.UserId, TenantId = login.ClientId, UserId = login.UserId }, cancellationToken: ct).ConfigureAwait(false); // ------------------------------------------------------------------------- // Insert a step-level detail row each time the user acts on a step. // The DTO must carry ShowRunStepId (PK) — ensure it is set before calling. // Parameters come from ShowRunStepDTO properties. // ------------------------------------------------------------------------- public async Task InsertShowRunStepAsync( ShowRunStepDTO dto, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync( login, ShowRunQB.INSERT_SHOW_RUN_STEP, dto, cancellationToken: ct).ConfigureAwait(false); // ------------------------------------------------------------------------- // Confirm the run belongs to this user before any mutation (ownership guard). // FIX: DatabaseName → TenantId // Parameters: @ShowRunId, @TenantId, @UserId // ------------------------------------------------------------------------- public async Task ValidateOwnerAsync( int showRunId, int userId, LoginDTO login, CancellationToken ct) { int count = await _qe.ExecuteScalarAsync( login, ShowRunQB.VALIDATE_SHOW_RUN_OWNER, new { ShowRunId = showRunId, TenantId = login.ClientId, UserId = userId }, cancellationToken: ct).ConfigureAwait(false); return count > 0; } // ------------------------------------------------------------------------- // Paginated run history for a user, optionally scoped to one show. // FIX: DatabaseName → TenantId // Parameters: @TenantId, @UserId, @ShowId, @PageNo, @PageSize // ------------------------------------------------------------------------- public async Task> GetHistoryAsync( int userId, int showId, int pageNo, int pageSize, LoginDTO login, CancellationToken ct) => await _qe.QueryAsync( login, ShowRunQB.GET_SHOW_RUN_HISTORY, new { TenantId = login.ClientId, UserId = userId, ShowId = showId, PageNo = pageNo, PageSize = pageSize }, cancellationToken: ct).ConfigureAwait(false); // ------------------------------------------------------------------------- // Total count for the paginated history query (same filters, no paging). // FIX: DatabaseName → TenantId // Parameters: @TenantId, @UserId, @ShowId // ------------------------------------------------------------------------- public async Task GetHistoryCountAsync( int userId, int showId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteScalarAsync( login, ShowRunQB.GET_SHOW_RUN_HISTORY_COUNT, new { TenantId = login.ClientId, UserId = userId, ShowId = showId }, cancellationToken: ct).ConfigureAwait(false); }