<commit_before>﻿using System;
using System.IO;
using System.Threading;

using KenticoInspector.Core.Models;
using KenticoInspector.Core.Services.Interfaces;

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace KenticoInspector.Core.Helpers
{
    public class ReportMetadataService : IReportMetadataService
    {
        public string DefaultCultureName => "en-US";

        public string CurrentCultureName => Thread.CurrentThread.CurrentCulture.Name;

        public ReportMetadataService(IInstanceService instanceService)
        {
            var metadataDirectory = $"{DirectoryHelper.GetExecutingDirectory()}\\{reportCodename}\\Metadata\\";
            
            var reportMetadata = GetReportMetadataInternal<T>(metadataDirectory, CurrentCultureName);

            var isCurrentCultureDefaultCulture = CurrentCultureName == DefaultCultureName;
            if (!isCurrentCultureDefaultCulture)
            {
                var defaultReportMetadata = GetReportMetadataInternal<T>(metadataDirectory, DefaultCultureName);
                reportMetadata = GetMergedMetadata(defaultReportMetadata, reportMetadata);
            }

            return reportMetadata;
        }


        private ReportMetadata<T> GetReportMetadataInternal<T>(string metadataDirectory, string cultureName) where T : new()
        {
            var reportMetadataPath = $"{metadataDirectory}{cultureName}.yaml";
            var reportMetadataPathExists = File.Exists(reportMetadataPath);
            return reportMetadataPathExists 
                ? DeserializeYaml<ReportMetadata<T>>(reportMetadataPath) 
                : new ReportMetadata<T>();
        }

        private T DeserializeYaml<T>(string path)
        {
            var deserializerBuilder = new DeserializerBuilder()
                .WithNamingConvention(new CamelCaseNamingConvention())
                .IgnoreUnmatchedProperties();

            var deserializer = deserializerBuilder<commit_msg>Fix random merge shenanigans in ReportMetadataService<commit_after>﻿using System;
using System.IO;
using System.Threading;

using KenticoInspector.Core.Models;
using KenticoInspector.Core.Services.Interfaces;

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace KenticoInspector.Core.Helpers
{
    public class ReportMetadataService : IReportMetadataService
    {
        public string DefaultCultureName => "en-US";

        public string CurrentCultureName => Thread.CurrentThread.CurrentCulture.Name;

        public ReportMetadata<T> GetReportMetadata<T>(string reportCodename) where T : new()
        {
            var metadataDirectory = $"{DirectoryHelper.GetExecutingDirectory()}\\{reportCodename}\\Metadata\\";

            var reportMetadata = GetReportMetadataInternal<T>(metadataDirectory, CurrentCultureName);

            var isCurrentCultureDefaultCulture = CurrentCultureName == DefaultCultureName;
            if (!isCurrentCultureDefaultCulture)
            {
                var defaultReportMetadata = GetReportMetadataInternal<T>(metadataDirectory, DefaultCultureName);
                reportMetadata = GetMergedMetadata(defaultReportMetadata, reportMetadata);
            }

            return reportMetadata;
        }


        private ReportMetadata<T> GetReportMetadataInternal<T>(string metadataDirectory, string cultureName) where T : new()
        {
            var reportMetadataPath = $"{metadataDirectory}{cultureName}.yaml";
            var reportMetadataPathExists = File.Exists(reportMetadataPath);
            return reportMetadataPathExists
                ? DeserializeYaml<ReportMetadata<T>>(reportMetadataPath)
                : new ReportMetadata<T>();
        }

        private T DeserializeYaml<T>(string path)
        {
            var deserializerBuilder = new DeserializerBuilder()
                .WithNamingConvention(new CamelCaseNamingConvention())
                .IgnoreUnmatchedProperties();

            var deserializer = deserializerBuilder
{'fraction_non_alphanumeric': 0.07472691161866932, 'fraction_numerical': 0.0, 'mean_word_length': 3.1070336391437308, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6345726', 'n_tokens_mistral': 951, 'n_tokens_neox': 926, 'n_words': 182}