<commit_before>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using NServiceBus;
using SFA.DAS.Commitments.Application.Interfaces;
using SFA.DAS.Commitments.Application.Interfaces.ApprenticeshipEvents;
using SFA.DAS.Commitments.Domain.Entities;
using SFA.DAS.Commitments.Domain.Interfaces;
using SFA.DAS.CommitmentsV2.Messages.Events;

namespace SFA.DAS.Commitments.Application.Services
{
    public class V2EventsPublisher : IV2EventsPublisher
    {
        private readonly IEndpointInstance _endpointInstance;
        private readonly ICommitmentsLogger _logger;


        public V2EventsPublisher(IEndpointInstance endpointInstance, ICommitmentsLogger logger)
        {
            _endpointInstance = endpointInstance;
            _logger = logger;
        }

        public async Task PublishApprenticeshipDeleted(Commitment commitment, Apprenticeship apprenticeship)
        {
            var logMessage = $"Publish IDraftApprenticeshipDeletedEvent message. Provider:{apprenticeship.ProviderId} Commitment:{commitment.Id} Apprenticeship:{apprenticeship.Id} ReservationId:{apprenticeship.ReservationId}";

            try
            {
                await _endpointInstance.Publish<IDraftApprenticeshipDeletedEvent>(ev =>
                {
                    ev.DraftApprenticeshipId = apprenticeship.Id;
                    ev.CohortId = commitment.Id;
                    ev.DeletedOn = DateTime.UtcNow;
                    ev.ReservationId = apprenticeship.ReservationId;
                    ev.Uln = apprenticeship.ULN;
                });

                _logger.Info($"{logMessage} successful");
            }
            catch(Exception e)
            {
                _logger.Error(e, $"{logMessage} failed");
            }
        }

        public async Task PublishApprenticeshipCreated(IApprenticeshipEvent apprenticeshipEvent)
        {
            var logMessage = $"Publish ApprenticeshipCreatedEvent message. Provider:{apprenticeshipEvent.Commitment.ProviderId} Commitment:{apprenticeshipEvent.Commitment.Id} Apprenticeship:{apprenticeshipEvent.Apprenticeship.Id}";

            try
            {
                var priceEpisodes = apprenticeshipEvent.Apprenticeship.PriceHistory
                    .Select(x => new PriceEpisode { FromDate = x.FromDate, ToDate = x.ToDate, Cost = x.Cost}).ToArray();

                await _endpointInstance.Publish<IApprenticeshipCreatedEvent>(ev =>
                {
                    ev.ApprenticeshipId = apprenticeshipEvent.Apprenticeship.Id;
                    ev.CreatedOn = DateTime.Now; // Get this from somewhere
                    ev.Uln = apprenticeshipEvent.Apprenticeship.ULN;
                    ev.ProviderId = apprenticeshipEvent.Apprenticeship.ProviderId;
                    ev.AccountId = apprenticeshipEvent.Apprenticeship.EmployerAccountId;
                    ev.AccountLegalEntityPublicHashedId =
                        apprenticeshipEvent.Apprenticeship.AccountLegalEntityPublicHashedId;
                    ev.LegalEntityName = apprenticeshipEvent.Apprenticeship.LegalEntityId; // This should be the Name
                    ev.StartDate = apprenticeshipEvent.Apprenticeship.StartDate.Value;<commit_msg>Changes due to Code Review<commit_after>﻿using System;
using System.Linq;
using System.Threading.Tasks;
using NServiceBus;
using SFA.DAS.Commitments.Application.Interfaces;
using SFA.DAS.Commitments.Application.Interfaces.ApprenticeshipEvents;
using SFA.DAS.Commitments.Domain.Entities;
using SFA.DAS.Commitments.Domain.Interfaces;
using SFA.DAS.CommitmentsV2.Messages.Events;

namespace SFA.DAS.Commitments.Application.Services
{
    public class V2EventsPublisher : IV2EventsPublisher
    {
        private readonly IEndpointInstance _endpointInstance;
        private readonly ICommitmentsLogger _logger;
        private readonly ICurrentDateTime _currentDateTime;


        public V2EventsPublisher(IEndpointInstance endpointInstance, ICommitmentsLogger logger, ICurrentDateTime currentDateTime)
        {
            _endpointInstance = endpointInstance;
            _logger = logger;
            _currentDateTime = currentDateTime;
        }

        public async Task PublishApprenticeshipDeleted(Commitment commitment, Apprenticeship apprenticeship)
        {
            var logMessage = $"Publish IDraftApprenticeshipDeletedEvent message. Provider:{apprenticeship.ProviderId} Commitment:{commitment.Id} Apprenticeship:{apprenticeship.Id} ReservationId:{apprenticeship.ReservationId}";

            try
            {
                await _endpointInstance.Publish<IDraftApprenticeshipDeletedEvent>(ev =>
                {
                    ev.DraftApprenticeshipId = apprenticeship.Id;
                    ev.CohortId = commitment.Id;
                    ev.DeletedOn = DateTime.UtcNow;
                    ev.ReservationId = apprenticeship.ReservationId;
                    ev.Uln = apprenticeship.ULN;
                });

                _logger.Info($"{logMessage} successful");
            }
            catch(Exception e)
            {
                _logger.Error(e, $"{logMessage} failed");
            }
        }

        public async Task PublishApprenticeshipCreated(IApprenticeshipEvent apprenticeshipEvent)
        {
            var logMessage = $"Publish ApprenticeshipCreatedEvent message. Provider:{apprenticeshipEvent.Commitment.ProviderId} Commitment:{apprenticeshipEvent.Commitment.Id} Apprenticeship:{apprenticeshipEvent.Apprenticeship.Id}";

            try
            {
                var priceEpisodes = apprenticeshipEvent.Apprenticeship.PriceHistory
                    .Select(x => new PriceEpisode { FromDate = x.FromDate, ToDate = x.ToDate, Cost = x.Cost}).ToArray();

                await _endpointInstance.Publish<IApprenticeshipCreatedEvent>(ev =>
                {
                    ev.ApprenticeshipId = apprenticeshipEvent.Apprenticeship.Id;
                    ev.CreatedOn = _currentDateTime.Now;
                    ev.Uln = apprenticeshipEvent.Apprenticeship.ULN;
                    ev.ProviderId = apprenticeshipEvent.Apprenticeship.ProviderId;
                    ev.AccountId = apprenticeshipEvent.Apprenticeship.EmployerAccountId;
                    ev.AccountLegalEntityPublicHashedId =
                        apprenticeshipEvent.Apprenticeship.AccountLegalEntityPublicHashedId;
                    ev.LegalEntityName = apprenticeshipEvent.Commitment.LegalEntityName;
                    ev.StartDate = apprenticeshipEvent.Apprenticeship.StartDate.Value;
{'fraction_non_alphanumeric': 0.07194244604316546, 'fraction_numerical': 0.001224552273075157, 'mean_word_length': 2.462639109697933, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2014785', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1709, 'n_words': 262}