package de.votian.services.service;

import de.votian.services.dto.InvoiceJobRowDto;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class InvoiceExportServiceTest {

    @Test
    void createCsvAddsServiceColumnWhenRowsContainPersistedServiceDetails() {
        InvoiceExportService service = new InvoiceExportService();
        InvoiceJobRowDto row = row();
        row.setServiceDetails("Fixpreis: 40.00\nServicepreis / Express: 5.00");

        String csv = new String(service.createCsv("Rechnungsausgabe", List.of(row)), StandardCharsets.UTF_8);

        assertThat(csv).contains("Exportiert;Leistungen;Rechnungstext");
        assertThat(csv).contains("\"Fixpreis: 40.00 Servicepreis / Express: 5.00\"");
    }

    @Test
    void createCsvKeepsLegacyColumnsWhenNoServiceDetailsExist() {
        InvoiceExportService service = new InvoiceExportService();

        String csv = new String(service.createCsv("Rechnungsausgabe", List.of(row())), StandardCharsets.UTF_8);

        assertThat(csv).contains("Exportiert;Rechnungstext");
        assertThat(csv).doesNotContain("Exportiert;Leistungen;Rechnungstext");
    }

    private InvoiceJobRowDto row() {
        InvoiceJobRowDto row = new InvoiceJobRowDto();
        row.setJobId(5L);
        row.setCustomerName("Musterkunde");
        row.setCostCenterName("KST 11");
        row.setOrderTime(LocalDateTime.of(2026, 3, 5, 8, 0));
        row.setFinishTime(LocalDateTime.of(2026, 3, 5, 9, 0));
        row.setCommissionNo("KOMM-1");
        row.setPickupName("Abholung");
        row.setDeliveryName("Zustellung");
        row.setCourierSid("CR-1");
        row.setTotalPrice(new BigDecimal("42.50"));
        row.setCourierPrice(new BigDecimal("25.00"));
        row.setMargin(new BigDecimal("17.50"));
        row.setExported(true);
        row.setInvoiceText("Freitext");
        return row;
    }
}
