using IWS5.DatabaseAccess.Interfaces.Importer;
using IWS5.DatabaseAccess.IwsDataContext;
using Microsoft.EntityFrameworkCore;
using IWS5.Importer.Abstractions.DTO;

namespace IWS5.DatabaseAccess.Repositories.Importer;

public class FolderFieldRepository(IDbContextFactory<GreenfieldPd2015Context> contextFactory)
    : IFolderFieldRepository
{
    public async Task<Dictionary<int, List<FolderFieldDto>>> GetFolderFieldsAsync()
    {
        await using var context = await contextFactory.CreateDbContextAsync();
        return await context.AtfieldDefinitions
            .GroupBy(f => f.ProcessId)
            .ToDictionaryAsync(
                g => g.Key,
                g => g.Select(f => new FolderFieldDto
                {
                    ProcessId = g.Key,
                    FieldName = f.FieldName,
                    DataType = f.DataType,
                    Id = f.FieldId
                }).ToList()
            );
    }
}
