using Hcs.Broker;
using Hcs.WebApp.Data.Hcs;
using Hcs.WebApp.Services;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;

namespace Hcs.WebApp.BackgroundServices.ResultGetters.Nsi
{
    public class ExportDataProviderNsiItemGetter_15_7_0_1(IClient client, IServiceScope scope, Operation operation) : ResultGetterBase(client, scope, operation)
    {
        public override async Task<ResultGetterResponse> GetAsync()
        {
            var result = await client.Nsi.GetExportDataProviderNsiItemResultAsync(operation.MessageGuid!);
            if (!result.Ready)
            {
                return new ResultGetterResponse()
                {
                    success = false
                };
            }

            if (result.Success)
            {
                DateTime endedAt = default;

                var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
                var registryService = scope.ServiceProvider.GetRequiredService<RegistryService>();

                using var context = headquartersService.GetNewContext();
                var executionStrategy = context.Database.CreateExecutionStrategy();
                await executionStrategy.ExecuteAsync(async () =>
                {
                    using var transaction = await context.Database.BeginTransactionAsync();
                    try
                    {
                        var registry = await registryService.GetRegistryByOperationIdAsync(context, operation.Id, true);
                        registry.SyncedAt = DateTime.UtcNow;
                        registry.Elements?.Clear();
                        await context.SaveChangesAsync();

                        registry.Elements ??= [];

                        foreach (var element in result.Result!.NsiElement)
                        {
                            var registryElement = new RegistryElement()
                            {
                                Code = element.Code,
                                GUID = element.GUID,
                                Json = JsonConvert.SerializeObject(element)
                            };
                            registry.Elements.Add(registryElement);
                        }
                        await context.SaveChangesAsync();

                        endedAt = DateTime.UtcNow;
                        await headquartersService.SetOperationEndedAsync(context, operation.Id, endedAt);

                        await transaction.CommitAsync();
                    }
                    catch
                    {
                        await transaction.RollbackAsync();

                        throw;
                    }
                });

                return new ResultGetterResponse()
                {
                    success = true,
                    endedAt = endedAt
                };
            }

            throw Failure(result.ErrorMessage);
        }
    }
}
