using IWS5.DatabaseAccess.IwsDataContext;
using IWS5.DatabaseAccess.IwsDataModels;
using Microsoft.EntityFrameworkCore;
using IWS5.Importer.Abstractions.Interfaces;
using IWS5.Importer.Abstractions.OptionsSetup;

namespace IWS5.DatabaseAccess.Repositories.Importer;

public class GeneralImportSettingsRepository(GreenfieldPd2015Context context)
    : IWSRepository<AtimporterConfiguration>(context), IGeneralImportSettingsRepository
{
    public async Task<GeneralImportSettingsDto?> GetGeneralSettingsAsync()
    {
        var config = await context.AtimporterConfigurations.FirstOrDefaultAsync(c => c.IsActive);
        var allowedTypes = await context.AtimportDocumentTypes
            .Where(t => t.IsActive)
            .Select(t => t.TypeName.ToLower())
            .ToListAsync();

        if (config == null) return null;

        return new GeneralImportSettingsDto
        {
            ImportDirs = config.DocumentPath,
            LogDir = config.LogPath,
            AllowedExtensions = allowedTypes,
            PauseTimeMinutes = config.LoopPauseMinutes,
            CopyLocation = config.ArchivePath,
            CopyErrorLocation = config.ErrorPath,
            CopyLife = config.CleanArchiveIntervalDays,
            LogDays = config.LogRetentionDays,
            LogSize = config.LogFileMaxSizeMb,
            CpuUsageInPercent = config.CpuUsagePercentLimit,
            MaxPdfSizeMB = config.MaxPdfsizeMb,
            CleanupIntervalHours = config.CleanArchiveIntervalDays
        };
    }
}
