package org.texttechnologylab.project.gruppe_05_1.database.domainimpl.mdb;

import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.database.MongoDBHandler;
import org.texttechnologylab.project.gruppe_05_1.database.MongoOperations;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.Mdb;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.MdbName;
import org.texttechnologylab.project.gruppe_05_1.domain.mdb.Wahlperiode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Mdb_Mongo_Impl extends Mdb implements MongoOperations<Mdb> {
    @Override
    public Document createEntity(Mdb entity) {
        Document bioDoc = factory.createBiografischeAngaben(entity.getBio());

        List<MdbName> namen= entity.getNamen();
        List<Document> namenDocs = new ArrayList<>();
        for (MdbName mdbName : namen) {
            namenDocs.add(factory.createMdbName(mdbName));
        }

        List<Wahlperiode> wps= entity.getWahlperioden();
        List<Document> wpDocs = new ArrayList<>();
        for (Wahlperiode wp : wps) {
            wpDocs.add(factory.createWahlperiode(wp));
        }

        Map<String, Object> fields = Map.of(
                "id",           entity.getId(),
                "namen",        namenDocs,
                "bio",          bioDoc,
                "wahlperioden", wpDocs
        );

        return MongoDBHandler.createDocument(false, fields);
    }

    @Override
    public List<Document> createList(List<Mdb> list) {
        List<Document> result = new ArrayList<>();
        for (Mdb mdb : list) {
            result.add(createEntity(mdb));
        }
        return result;
    }
}
