private async Task <SelfAssesmentModel> BuildSelfAssesmentModel(long id, string name, EstablishmentType financeType, EdubaseDataObject schoolContextData, SchoolTrustFinancialDataObject schoolFinancialData)
        {
            string termYears = await GetLatestTermYears(financeType);

            SelfAssesmentModel model;

            if (schoolFinancialData is not null)
            {
                model = new SelfAssesmentModel(
                    id,
                    name,
                    schoolFinancialData.OverallPhase,
                    financeType.ToString(),
                    schoolFinancialData.LondonWeight,
                    schoolFinancialData.NoPupils.GetValueOrDefault(),
                    schoolFinancialData.PercentageFSM.GetValueOrDefault(),
                    schoolContextData.OfstedRating,
                    FormatOfstedDate(schoolContextData.OfstedLastInsp),
                    schoolFinancialData.Progress8Measure,
                    schoolFinancialData.Ks2Progress,
                    GetProgressScoreType(schoolFinancialData),
                    schoolFinancialData.Progress8Banding.GetValueOrDefault(),
                    bool.Parse(schoolFinancialData.Has6Form),
                    schoolFinancialData.TotalExpenditure.GetValueOrDefault(),
                    schoolFinancialData.TotalIncome.GetValueOrDefault(),
                    termYears,
                    schoolFinancialData.TeachersTotal.GetValueOrDefault(),
                    schoolFinancialData.TeachersLeader.GetValueOrDefault(),
                    schoolFinancialData.WorkforceTotal.GetValueOrDefault(),
                    schoolFinancialData.PeriodCoveredByReturn >= 12,
                    true
                    );

                model.SadSizeLookup = await _selfAssesmentDashboardDataService.GetSADSizeLookupDataObject(schoolFinancialData.OverallPhase, bool.Parse(schoolFinancialData.Has6Form), schoolFinancialData.NoPupils.GetValueOrDefault(), termYears);

                model.SadFSMLookup = await _selfAssesmentDashboardDataService.GetSADFSMLookupDataObject(schoolFinancialData.OverallPhase, bool.Parse(schoolFinancialData.Has6Form), schoolFinancialData.PercentageFSM.GetValueOrDefault(), termYears);

                model.AvailableScenarioTerms = await GetAllAvailableTermYears();
            }
            else
            {
                model = new SelfAssesmentModel(id, name, schoolContextData.OverallPhase, financeType.ToString(), schoolContextData.OfstedRating,
                                               FormatOfstedDate(schoolContextData.OfstedLastInsp),
                                               schoolContextData.GovernmentOfficeRegion,
                                               schoolContextData.OfficialSixthForm);

                model.AvailableScenarioTerms = await GetAllAvailableTermYears();
            }

            await AddAssessmentAreasToModel(termYears, schoolFinancialData, schoolContextData, model);

            return(model);
        }
        private async Task AddAssessmentArea(string areaType, string areaName, decimal?schoolData, decimal?totalForAreaType, SchoolTrustFinancialDataObject schoolFinancialData, SelfAssesmentModel model, string termYears)
        {
            List <SADSchoolRatingsDataObject> ratings = await _selfAssesmentDashboardDataService.GetSADSchoolRatingsDataObjectAsync(areaName, schoolFinancialData?.OverallPhase, bool.Parse(schoolFinancialData?.Has6Form ?? "false"), schoolFinancialData?.LondonWeight, model.SadSizeLookup?.SizeType, model.SadFSMLookup?.FSMScale, termYears);

            ratings = ratings.OrderBy(t => t.ScoreLow).ToList();
            model.SadAssesmentAreas.Add(new SadAssesmentAreaModel(areaType, areaName, schoolData, totalForAreaType, ratings));
        }
        private async Task AddAssessmentAreasToModel(string termYears, SchoolTrustFinancialDataObject schoolFinancialData, EdubaseDataObject schoolContextData, SelfAssesmentModel model)
        {
            model.SadAssesmentAreas = new(); //C#9

            await AddAssessmentArea("Spending", "Teaching staff", schoolFinancialData?.TeachingStaff.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Supply staff", schoolFinancialData?.SupplyStaff.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Education support staff", schoolFinancialData?.EducationSupportStaff.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Administrative and clerical staff", schoolFinancialData?.AdministrativeClericalStaff.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Other staff costs", schoolFinancialData?.OtherStaffCosts.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Premises costs", schoolFinancialData?.Premises.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Educational supplies", schoolFinancialData?.EducationalSupplies.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Spending", "Energy", schoolFinancialData?.Energy.GetValueOrDefault(), schoolFinancialData?.TotalExpenditure.GetValueOrDefault(), schoolFinancialData, model, termYears);

            await AddAssessmentArea("Reserve and balance", "In-year balance", schoolFinancialData?.InYearBalance.GetValueOrDefault(), schoolFinancialData?.TotalIncome.GetValueOrDefault(), schoolFinancialData, model, termYears);
            await AddAssessmentArea("Reserve and balance", "Revenue reserve", schoolFinancialData?.RevenueReserve.GetValueOrDefault(), schoolFinancialData?.TotalIncome.GetValueOrDefault(), schoolFinancialData, model, termYears);

            await AddAssessmentArea("School characteristics", "Average teacher cost", null, null, schoolFinancialData, model, termYears);
            await AddAssessmentArea("School characteristics", "Senior leaders as a percentage of workforce", null, null, schoolFinancialData, model, termYears);;;
            await AddAssessmentArea("School characteristics", "Pupil to teacher ratio", null, null, schoolFinancialData, model, termYears);
            await AddAssessmentArea("School characteristics", "Pupil to adult ratio", null, null, schoolFinancialData, model, termYears);

            if (!_exclusionPhaseList.Contains(schoolFinancialData?.OverallPhase ?? schoolContextData.OverallPhase))
            {
                await AddAssessmentArea("School characteristics", "Teacher contact ratio (less than 1)", null, null, schoolFinancialData, model, termYears);
            }

            await AddAssessmentArea("School characteristics", "Predicted percentage pupil number change in 3-5 years", null, null, schoolFinancialData, model, termYears);

            if (!_exclusionPhaseList.Contains(schoolFinancialData?.OverallPhase ?? schoolContextData.OverallPhase))
            {
                await AddAssessmentArea("School characteristics", "Average Class size", null, null, schoolFinancialData, model, termYears);
            }
        }