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

namespace IWS5.DatabaseAccess.Repositories.Importer;

public class DocumentFieldRepository(IDbContextFactory<GreenfieldPd2015Context> contextFactory)
    : IDocumentFieldRepository
{
    public async Task<Dictionary<int, List<DocumentFieldDto>>> GetDocumentFieldsAsync()
    {
        await using var context = await contextFactory.CreateDbContextAsync();

        return await context.AtdocumentCabinetDefs
            .AsNoTracking()
            .GroupBy(d => d.CabinetId)
            .ToDictionaryAsync(
                g => g.Key,
                g => g.Select(d => new DocumentFieldDto
                {
                    CabinetId = g.Key,
                    FieldName = d.FieldName,
                    FieldDataType = d.FieldDataType
                }).ToList()
            );
    }
}
