using IWS5.Core.Enums;
using IWS5.Core.Models.Processing;
using Microsoft.Data.SqlClient;
using ILogger = Serilog.ILogger;

namespace IWS5.Server.EventProcessors;

public class StartProcessor(ILogger logger, IConfiguration configuration) : EventProcessor(logger, configuration)
{
    public override async Task<bool> ProcessEventAsync(EventModel eventData, int processId)
    {
        await using(var sqlConnection = new SqlConnection(ConnectionString))
        {
            await sqlConnection.OpenAsync();
            
            var sqlStr =
                $@"UPDATE ATWorkItemReference 
                   SET EventID = {eventData.NextEventId}, 
                       PendEventId = 0, 
                       PendQueueId = null, 
                       PendExpirationTime = NULL, 
                       ServerRequest = {(int)ServerRequest.ForwardExistingWorkItem}, 
                       ServerRequestParam = NULL,
                       ArrivalDate = '{DateTime.Now}',
                       LastActionDate = '{DateTime.Now}'
                   WHERE WorkItemID = {eventData.WorkItemId}";

            SqlCommand cmd2 = new SqlCommand(sqlStr, sqlConnection);
            await cmd2.ExecuteNonQueryAsync();
            
            return true;
        }
    }
}