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

namespace IWS5.Server.EventProcessors;

public class StopProcessor(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.CurrentEventId}, 
                       PendQueueId = null, 
                       PendEventId = 0, 
                       PendExpirationTime = NULL, 
                       ServerRequest = {(int)ServerRequest.FinishWorkItem}, 
                       LastActionDate = '{DateTime.Now}'
                   WHERE WorkItemID = {eventData.WorkItemId}";

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

            return true;
        }
    }
}