package at.procon.dip.domain.ted.search.integration;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import at.procon.dip.domain.document.DocumentFamily;
import at.procon.dip.domain.document.DocumentType;
import at.procon.dip.domain.document.RepresentationType;
import at.procon.dip.domain.ted.entity.TedNoticeProjection;
import at.procon.dip.testsupport.AbstractTedStructuredSearchIntegrationTest;
import at.procon.ted.model.entity.ContractNature;
import at.procon.ted.model.entity.NoticeType;
import at.procon.ted.model.entity.ProcedureType;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import org.junit.jupiter.api.Test;

class TedStructuredSearchEndpointIntegrationTest extends AbstractTedStructuredSearchIntegrationTest {

    @Test
    void getSearch_should_return_structured_results_and_facets() throws Exception {
        var created = dataFactory.createDocumentWithPrimaryRepresentation(
                "Medical imaging systems for Vienna hospital",
                "Procurement summary",
                "Imaging systems and maintenance.",
                DocumentType.TED_NOTICE,
                DocumentFamily.PROCUREMENT,
                "en",
                RepresentationType.SEMANTIC_TEXT
        );

        tedNoticeProjectionRepository.save(TedNoticeProjection.builder()
                .document(created.document())
                .publicationId("100000-2026")
                .noticeId("notice-100000-2026")
                .noticeType(NoticeType.CONTRACT_NOTICE)
                .buyerName("Vienna General Hospital")
                .buyerCountryCode("AUT")
                .buyerCity("Vienna")
                .projectTitle("Medical imaging systems")
                .contractNature(ContractNature.SUPPLIES)
                .procedureType(ProcedureType.OPEN)
                .publicationDate(LocalDate.of(2026, 4, 10))
                .submissionDeadline(OffsetDateTime.parse("2026-05-01T10:00:00+02:00"))
                .cpvCodes(new String[]{"33110000", "33120000"})
                .totalLots(2)
                .euFunded(true)
                .build());

        mockMvc.perform(get("/api/v1/documents/search")
                        .param("countryCode", "AUT")
                        .param("noticeType", "CONTRACT_NOTICE")
                        .param("includeFacets", "true"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.documents[0].publicationId").value("100000-2026"))
                .andExpect(jsonPath("$.documents[0].buyerName").value("Vienna General Hospital"))
                .andExpect(jsonPath("$.facets.countries[0].key").value("AUT"))
                .andExpect(jsonPath("$.facets.noticeTypes[0].key").value("CONTRACT_NOTICE"));
    }
}
