using Hcs.WebApp.Data.Hcs;
using Microsoft.EntityFrameworkCore;

namespace Hcs.WebApp.Services
{
    public class HeadquartersService(IDbContextFactory<HcsDbContext> factory) : HcsServiceBase(factory)
    {
        public async Task<bool> HasActiveCampaignAsync(Campaign.CampaignType type)
        {
            using var context = GetNewContext();
            return await context.Campaigns.AnyAsync(x => x.Type == type && !x.EndedAt.HasValue);
        }

        public async Task<IEnumerable<Campaign>> GetCampaignsAsync()
        {
            using var context = GetNewContext();
            return await context.Campaigns.ToListAsync();
        }

        public async Task<IEnumerable<Campaign>> GetNotEndedCampaignsAsync()
        {
            using var context = GetNewContext();
            return await (from campaign in context.Campaigns
                          where !campaign.EndedAt.HasValue
                          select campaign).ToListAsync();
        }

        public async Task<IEnumerable<Operation>> GetNotExecutedOperationsAsync()
        {
            using var context = GetNewContext();
            return await (from operation in context.Operations
                          where string.IsNullOrEmpty(operation.MessageGuid) && !operation.EndedAt.HasValue
                          select operation).ToListAsync();
        }

        public async Task<IEnumerable<Operation>> GetResultWaitingOperationsAsync()
        {
            using var context = GetNewContext();
            return await (from operation in context.Operations
                          where !string.IsNullOrEmpty(operation.MessageGuid) && !operation.EndedAt.HasValue
                          select operation).ToListAsync();
        }

        public async Task<IEnumerable<Operation>> GetOperationsAsync(int campaignId)
        {
            using var context = GetNewContext();
            return await (from operation in context.Operations
                          where operation.CampaignId == campaignId
                          select operation).ToListAsync();
        }

        public async Task<Campaign> InitiateCampaignAsync(Campaign.CampaignType type, string initiatorId)
        {
            using var context = GetNewContext();
            var campaign = new Campaign()
            {
                Type = type,
                InitiatorId = initiatorId,
                CreatedAt = DateTime.UtcNow
            };
            await context.Campaigns.AddAsync(campaign);
            await context.SaveChangesAsync();
            return campaign;
        }

        public async Task<IEnumerable<Operation>> InitiateOperationsAsync(HcsDbContext context, int count, int campaignId, Operation.OperationType type)
        {
            var operations = new List<Operation>();
            for (var i = 0; i < count; i++)
            {
                operations.Add(new Operation()
                {
                    CampaignId = campaignId,
                    Type = type,
                    CreatedAt = DateTime.UtcNow
                });
            }
            await context.Operations.AddRangeAsync(operations);
            await context.SaveChangesAsync();
            return operations;
        }

        public async Task SetCampaignStartedAsync(HcsDbContext context, int campaignId)
        {
            var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
            if (campaign != null)
            {
                campaign.StartedAt = DateTime.UtcNow;

                await context.SaveChangesAsync();
            }
        }

        public async Task SetOperationStartedAsync(int operationId)
        {
            using var context = GetNewContext();
            var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
            if (operation != null)
            {
                operation.StartedAt = DateTime.UtcNow;

                await context.SaveChangesAsync();
            }
        }

        public async Task SetCampaignEndedAsync(int campaignId)
        {
            using var context = GetNewContext();
            await SetCampaignEndedAsync(context, campaignId);
        }

        public async Task SetCampaignEndedAsync(HcsDbContext context, int campaignId)
        {
            var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
            if (campaign != null)
            {
                campaign.EndedAt = DateTime.UtcNow;

                await context.SaveChangesAsync();
            }
        }

        public async Task SetCampaignEndedWithFailAsync(int campaignId, string failureReason)
        {
            using var context = GetNewContext();
            var campaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaignId);
            if (campaign != null)
            {
                campaign.EndedAt = DateTime.UtcNow;
                campaign.FailureReason = failureReason;

                await context.SaveChangesAsync();
            }
        }

        public async Task SetOperationEndedAsync(HcsDbContext context, int operationId)
        {
            var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
            if (operation != null)
            {
                operation.EndedAt = DateTime.UtcNow;

                await context.SaveChangesAsync();
            }
        }

        public async Task SetOperationEndedWithFailAsync(int operationId, string failureReason)
        {
            using var context = GetNewContext();
            var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
            if (operation != null)
            {
                operation.EndedAt = DateTime.UtcNow;
                operation.FailureReason = failureReason;

                await context.SaveChangesAsync();
            }
        }

        public async Task SetOperationMessageGuidAsync(int operationId, string messageGuid)
        {
            using var context = GetNewContext();
            var operation = await context.Operations.FirstOrDefaultAsync(x => x.Id == operationId);
            if (operation != null)
            {
                operation.MessageGuid = messageGuid;

                await context.SaveChangesAsync();
            }
        }

        public async Task UpdateCampaignStepAsync(Campaign campaign)
        {
            using var context = GetNewContext();
            await UpdateCampaignStepAsync(context, campaign);
        }

        public async Task UpdateCampaignStepAsync(HcsDbContext context, Campaign campaign)
        {
            var targetCampaign = await context.Campaigns.FirstOrDefaultAsync(x => x.Id == campaign.Id);
            if (targetCampaign != null)
            {
                targetCampaign.Step = campaign.Step;

                await context.SaveChangesAsync();
            }
        }
    }
}
