package com.knecon.fforesight.service.layoutparser.processor.services;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.stereotype.Service;

import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.SimplifiedSectionText;
import com.knecon.fforesight.service.layoutparser.internal.api.data.redaction.SimplifiedText;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.Document;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.GenericSemanticNode;
import com.knecon.fforesight.service.layoutparser.processor.model.graph.nodes.SemanticNode;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SimplifiedSectionTextService {

    public SimplifiedText toSimplifiedText(Document document) {

        List<SimplifiedSectionText> simplifiedMainSectionsList = document.getMainSections()
                .stream()
                .map(this::toSimplifiedSectionText)
                .toList();
        List<SimplifiedSectionText> simplifiedHeadersList = document.getHeaders()
                .stream()
                .map(this::toSimplifiedSectionText)
                .toList();
        List<SimplifiedSectionText> simplifiedFootersList = document.getFooters()
                .stream()
                .map(this::toSimplifiedSectionText)
                .toList();
        List<SimplifiedSectionText> simplifiedText = Stream.of(simplifiedMainSectionsList, simplifiedHeadersList, simplifiedFootersList)
                .flatMap(List::stream)
                .collect(Collectors.toList());
        return SimplifiedText.builder().numberOfPages(document.getNumberOfPages()).sectionTexts(simplifiedText).build();
    }


    private SimplifiedSectionText toSimplifiedSectionText(SemanticNode section) {

        return SimplifiedSectionText.builder()
                .sectionNumber(section.getTreeId()
                                       .get(0))
                .text(section.getTextBlock().getSearchText())
                .build();
    }

}
