package de.assecutor.votianlt.service;

import de.assecutor.votianlt.model.SystemCompanyProfile;
import de.assecutor.votianlt.model.invoices.SystemInvoiceData;
import de.assecutor.votianlt.repository.SystemCompanyProfileRepository;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * Manages the company data of the system operator. As long as no profile has
 * been saved, the defaults from SystemInvoiceData are used.
 */
@Service
public class SystemCompanyProfileService {

    private final SystemCompanyProfileRepository repository;

    public SystemCompanyProfileService(SystemCompanyProfileRepository repository) {
        this.repository = repository;
    }

    /**
     * Returns the stored profile, or a new unsaved instance prefilled with the
     * SystemInvoiceData defaults for first-time editing.
     */
    public SystemCompanyProfile getOrCreateProfile() {
        Optional<SystemCompanyProfile> stored = repository.findAll().stream().findFirst();
        if (stored.isPresent()) {
            return stored.get();
        }
        SystemInvoiceData defaults = new SystemInvoiceData();
        SystemCompanyProfile profile = new SystemCompanyProfile();
        profile.setCompanyName(defaults.getCompanyName());
        profile.setCompanySubtitle(defaults.getCompanySubtitle());
        profile.setCompanyStreet(defaults.getCompanyStreet());
        profile.setCompanyCity(defaults.getCompanyCity());
        profile.setCompanyPhone(defaults.getCompanyPhone());
        profile.setCompanyFax(defaults.getCompanyFax());
        profile.setCompanyEmail(defaults.getCompanyEmail());
        profile.setCompanyWebsite(defaults.getCompanyWebsite());
        profile.setSenderLine(defaults.getSenderLine());
        profile.setPaymentTerms(defaults.getPaymentTerms());
        profile.setFooterText(defaults.getFooterText());
        return profile;
    }

    public SystemCompanyProfile save(SystemCompanyProfile profile) {
        return repository.save(profile);
    }

    /**
     * Creates a SystemInvoiceData whose issuer fields come from the stored
     * profile. Once a profile exists it is the source of truth for all issuer
     * fields; without one the hardcoded defaults remain.
     */
    public SystemInvoiceData createSystemInvoiceData() {
        SystemInvoiceData data = new SystemInvoiceData();
        repository.findAll().stream().findFirst().ifPresent(profile -> {
            data.setCompanyName(nvl(profile.getCompanyName()));
            data.setCompanySubtitle(nvl(profile.getCompanySubtitle()));
            data.setCompanyStreet(nvl(profile.getCompanyStreet()));
            data.setCompanyCity(nvl(profile.getCompanyCity()));
            data.setCompanyPhone(nvl(profile.getCompanyPhone()));
            data.setCompanyFax(nvl(profile.getCompanyFax()));
            data.setCompanyEmail(nvl(profile.getCompanyEmail()));
            data.setCompanyWebsite(nvl(profile.getCompanyWebsite()));
            data.setSenderLine(nvl(profile.getSenderLine()));
            data.setPaymentTerms(nvl(profile.getPaymentTerms()));
            data.setFooterText(nvl(profile.getFooterText()));
        });
        return data;
    }

    private String nvl(String value) {
        return value != null ? value : "";
    }
}
