/*
 * Copyright (c) 2025 Rostock University Library
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
package de.uni.rostock.ub.wsrb.edition.webapp.controller;

import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import de.uni.rostock.ub.wsrb.edition.webapp.service.SolrService;
import de.uni.rostock.ub.wsrb.edition.webapp.service.XSLTDocumentationService;
import de.uni.rostock.ub.wsrb.edition.webapp.service.XSLTEditionService;
import de.uni.rostock.ub.wsrb.edition.webapp.service.XSLTTexteinheitService;
import de.uni.rostock.ub.wsrb.edition.webapp.util.SearchResult;

@Controller
public class EditionController {

    @Autowired
    XSLTEditionService xsltEditionService;

    @Autowired
    XSLTTexteinheitService xsltTexteinheitService;

    @Autowired
    XSLTDocumentationService xsltDocumentationService;

    @Autowired
    SolrService solrService;

    @GetMapping(path = "/edition")
    ModelAndView edition(@RequestParam(name = "id", defaultValue = "") List<String> id,
        @RequestParam(name = "filter", defaultValue = "") List<String> filter,

        @RequestParam(name = "searchRegion", defaultValue = "") String searchRegion,
        @RequestParam(name = "searchTerm", defaultValue = "") String searchTerm) {

        ModelAndView mav = new ModelAndView("edition");
        mav.addObject("taxLabels", xsltEditionService.getTaxLabels());
        mav.addObject("html4BiblPopups", xsltDocumentationService.getHtmlBiblPopups("."));

        SearchResult searchResult = solrService.searchEdition(id, filter, searchRegion, searchTerm);
        mav.addObject("filteredSprueche",
            xsltEditionService.createFilteredSprueche(searchResult.ids(), List.of(), searchResult.highlightMap()));
        mav.addObject("resultIDs", String.join(", ", searchResult.ids()));
        SortedMap<String, Long> facetMap = new TreeMap<>(searchResult.facets());
        mav.addObject("resultFacets", facetMap);

        return mav;
    }

    @GetMapping(path = "/texteinheit")
    @ResponseBody
    String transcription(
        @RequestParam(name = "te_ids", defaultValue = "") String ids,
        @RequestParam(name = "te_labels", defaultValue = "") String labels) {
        return xsltTexteinheitService.createTranscriptionFragments(ids, labels);
    }

    @GetMapping(path = "/solr4edition", produces = MediaType.TEXT_XML_VALUE)
    @ResponseBody
    public String createSolr4Edition() {
        return xsltEditionService.createSolr4Edition();
    }

    @GetMapping(path = "/solr-reload", produces = MediaType.TEXT_HTML_VALUE)
    @ResponseBody
    public String reloadSolr() {
        solrService.reloadWSRBSolrData();
        return ("""
            <html lang="en">
              <head>
                 <meta charset="utf-8" />
                 <title>WSRB Solr Response</title>
              </head>
              <body>
                <h1>Solr-Reload: OK</h1>
              </body>
            </html>
            """);
    }
}
