<commit_before>﻿using AutoMapper;
using Esfa.Vacancy.Api.Types;
using Esfa.Vacancy.Register.Domain.Entities;

namespace Esfa.Vacancy.Register.Api.Mappings
{
    public class VacancyMappings : Profile
    {
        private const string UnknownText = "Unknown";

        public VacancyMappings()
        {
            CreateMap<Domain.Entities.Vacancy, Vacancy.Api.Types.Vacancy>()
                .ForMember(apiType => apiType.VacancyUrl, opt => opt.Ignore())
                .ForMember(apiType => apiType.Wage, opt => opt.Ignore())
                .ForMember(apiType => apiType.VacancyType, opt => opt.MapFrom(source => source.VacancyTypeId))
                .ForMember(apiType => apiType.WageUnit, opt => opt.MapFrom(source => source.WageUnitId))
                .ForMember(apiType => apiType.VacancyReference, opt => opt.MapFrom(source => source.VacancyReferenceNumber))
                .ForMember(apiType => apiType.LocationType, opt => opt.MapFrom(source => source.VacancyLocationTypeId))
                .AfterMap((src, dest) =>
                {
                    if (src.IsAnonymousEmployer && (VacancyStatus)src.VacancyStatusId == VacancyStatus.Live)
                    {
                        ApplyAnonymisationToVacancy(src, dest);
                    }

                    if (dest.VacancyType == VacancyType.Traineeship)
                    {
                        ResetWageFieldsForTraineeship(dest);
                    }
                    else
                    {
                        AdjustWageUnitBasedOnWageType(src, dest);

                        MapWageFields(src, dest);
                    }
                });
        }

        private void MapWageFields(Domain.Entities.Vacancy src, Vacancy.Api.Types.Vacancy dest)
        {
            switch (src.WageType)
            {
                case (int) WageType.LegacyText:
                    dest.Wage = UnknownText;
                    break;
                case (int) WageType.LegacyWeekly:
                case (int)WageType.Custom:
                    dest.Wage = src.WeeklyWage?.ToString("C0") ?? UnknownText;
                    break;
                case (int) WageType.ApprenticeshipMinimum:

                    break;
                case (int) WageType.NationalMinimum:

                    break;
                case (int) WageType.CustomRange:
                    dest.Wage = $"{src.WageLowerBound?.ToString("C0") ?? UnknownText} - {src.WageUpperBound?.ToString("C0") ?? UnknownText}";
                    break;
                case (int) WageType.CompetitiveSalary:
                    dest.Wage = "Competitive salary";
                    break;
                case (int) WageType.ToBeAgreedUponAppointment:
                    dest.Wage = "To be agreed upon appointment";
                    break;
                case (int) WageType.Unwaged:
                    dest.Wage = "Unwaged";
                    break;
            }
        }

        private void AdjustWageUnitBasedOnWageType(Domain.Entities.Vacancy src, Vacancy.Api.Types.Vacancy dest)
        {
            switch (src.WageType)
            {
                case (int) WageType.Custom:
                    break;
                case (int) WageType.CustomRange:
                    dest.WageUnit = src.WageUnitId.HasValue && (WageUnit) src.WageUnitId.Value == WageUnit.NotApplicable
                        ? WageUnit.Weekly
                        : dest.WageUnit;
                    break;
                case (int) WageType.LegacyWeekly:
                    dest.WageUnit = WageUnit.Weekly;
                    break;
                default:
                    dest.WageUnit = null;
                    break;
            }
        }

        private void ResetWageFieldsForTraineeship(Vacancy.Api.Types.Vacancy dest)
        {
            dest.Wage = null;
            dest.WageUnit = null;
            dest.HoursPerWeek = null;
        }
<commit_msg>RA-1174 - Updated vacancy mapping around wages.<commit_after>﻿using AutoMapper;
using Esfa.Vacancy.Api.Types;
using Esfa.Vacancy.Register.Domain.Entities;

namespace Esfa.Vacancy.Register.Api.Mappings
{
    public class VacancyMappings : Profile
    {
        private const string UnknownText = "Unknown";
        private const string CurrencyStringFormat = "C";

        public VacancyMappings()
        {
            CreateMap<Domain.Entities.Vacancy, Vacancy.Api.Types.Vacancy>()
                .ForMember(apiType => apiType.VacancyUrl, opt => opt.Ignore())
                .ForMember(apiType => apiType.Wage, opt => opt.Ignore())
                .ForMember(apiType => apiType.VacancyType, opt => opt.MapFrom(source => source.VacancyTypeId))
                .ForMember(apiType => apiType.WageUnit, opt => opt.MapFrom(source => source.WageUnitId))
                .ForMember(apiType => apiType.VacancyReference, opt => opt.MapFrom(source => source.VacancyReferenceNumber))
                .ForMember(apiType => apiType.LocationType, opt => opt.MapFrom(source => source.VacancyLocationTypeId))
                .AfterMap((src, dest) =>
                {
                    if (src.IsAnonymousEmployer && (VacancyStatus)src.VacancyStatusId == VacancyStatus.Live)
                    {
                        ApplyAnonymisationToVacancy(src, dest);
                    }

                    if (dest.VacancyType == VacancyType.Traineeship)
                    {
                        ResetWageFieldsForTraineeship(dest);
                    }
                    else
                    {
                        AdjustWageUnitBasedOnWageType(src, dest);

                        MapWageFields(src, dest);
                    }
                });
        }

        private void MapWageFields(Domain.Entities.Vacancy src, Vacancy.Api.Types.Vacancy dest)
        {
            switch (src.WageType)
            {
                case (int) WageType.LegacyText:
                    dest.Wage = UnknownText;
                    break;
                case (int) WageType.LegacyWeekly:
                case (int)WageType.Custom:
                    dest.Wage = src.WeeklyWage?.ToString(CurrencyStringFormat) ?? UnknownText;
                    break;
                case (int) WageType.ApprenticeshipMinimum:
                    dest.Wage = src.MinimumWageRate.HasValue && src.HoursPerWeek.HasValue 
                                ? (src.MinimumWageRate.Value * src.HoursPerWeek.Value).ToString(CurrencyStringFormat) 
                                : UnknownText;
                    break;
                case (int) WageType.NationalMinimum:
                    dest.Wage = GetNationalMinimumWageRangeText(src);
                    break;
                case (int) WageType.CustomRange:
                    dest.Wage = GetWageRangeText(src);
                    break;
                case (int) WageType.CompetitiveSalary:
                    dest.Wage = "Competitive salary";
                    break;
                case (int) WageType.ToBeAgreedUponAppointment:
                    dest.Wage = "To be agreed upon appointment";
                    break;
                case (int) WageType.Unwaged:
                    dest.Wage = "Unwaged";
                    break;
            }
        }

        private string GetWageRangeText(Domain.Entities.Vacancy src)
        {
            return $"{src.WageLowerBound?.ToString(CurrencyStringFormat) ?? UnknownText} - {src.WageUpperBound?.ToString(CurrencyStringFormat) ?? UnknownText}";
        }

        private string GetNationalMinimumWageRangeText(Domain.Entities.Vacancy src)
        {
            if (!src.HoursPerWeek.HasValue || src.HoursPerWeek <= 0)
            {
                return UnknownText;
            }

            if (!src.MinimumWageLowerBound.HasValue && !src.MinimumWageUpperBound.HasValue)
            {
                return UnknownText;
            }

            var lowerMinimumLimit = src.MinimumWageLowerBound * src.HoursPerWeek;
            var upperMinimumLimit = src.MinimumWageUpperBound * src.HoursPerWeek;
            
            var minLowerBoundSection = lowerMinimumLimit?.ToString(CurrencyStringFormat) ?? UnknownText;
            var minUpperBoundSection = upperMinimumLimit?.ToString(CurrencyStringFormat) ?? UnknownText;

            return $"{minLowerBoundSection} - {minUpperBoundSection}";
        }

        private void AdjustWageUnitBasedOnWageType(Domain.Entities.Vacancy src, Vacancy.Api.Types.Vacancy dest)
        {
            switch (src.WageType)
            {
                case (int) WageType.Custom:
                    break;
                case (int) WageType.CustomRange:
                    dest.WageUnit = src.WageUnitId.HasValue && (WageUnit) src.WageUnitId.Value == WageUnit.NotApplicable
                        ? WageUnit.Weekly
                        : dest.WageUnit;
                    break;
                case (int) WageType.LegacyWeekly:
                    dest.WageUnit = WageUnit.Weekly;
                    break;
                default:
                    dest.WageUnit = null;
                    break;
            }
        }

        private void ResetWageFieldsForTraineeship(Vacancy.Api.Types.Vacancy dest)
        {
            dest.Wage = null;
            dest.WageUnit = null;
            dest.HoursPerWeek = null;
        }

{'fraction_non_alphanumeric': 0.08888174118151602, 'fraction_numerical': 0.0008577248847432186, 'mean_word_length': 1.4450851900393185, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4737491', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2448, 'n_words': 402}