package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import de.assecutor.votianlt.model.PriceTable;
import de.assecutor.votianlt.pages.base.ui.view.AdminLayout;
import de.assecutor.votianlt.repository.PriceTableRepository;
import jakarta.annotation.security.RolesAllowed;

@Route(value = "admin-price-table", layout = AdminLayout.class)
@PageTitle("Preis-Tabelle")
@RolesAllowed("ADMIN")
public class AdminPricetableView extends VerticalLayout {

    private final PriceTableRepository priceTableRepository;
    private final TextField monthlyBasePackage;
    private final TextField appUsageLicense;
    private final TextField revenueParticipation;

    public AdminPricetableView(PriceTableRepository priceTableRepository) {
        this.priceTableRepository = priceTableRepository;

        setSpacing(false);
        setPadding(false);
        getStyle().set("margin", "14px");
        setWidth("90%");

        H2 title = new H2("Preis-Tabelle");
        add(title);

        VerticalLayout fieldsLayout = new VerticalLayout();
        fieldsLayout.setSpacing(true);
        fieldsLayout.setPadding(false);

        monthlyBasePackage = new TextField("Monatliche Grundpauschale");
        monthlyBasePackage.setWidth("40%");
        monthlyBasePackage.setMaxWidth("40%");

        appUsageLicense = new TextField("App-Nutzungslizenz");
        appUsageLicense.setWidth("40%");
        appUsageLicense.setMaxWidth("40%");

        revenueParticipation = new TextField("Umsatzbeteiligung in Prozent");
        revenueParticipation.setWidth("40%");
        revenueParticipation.setMaxWidth("40%");

        fieldsLayout.add(monthlyBasePackage, appUsageLicense, revenueParticipation);

        add(fieldsLayout);

        Button saveButton = new Button("Speichern");
        saveButton.getStyle().set("margin-top", "20px");
        saveButton.addClickListener(e -> savePriceTable());

        add(saveButton);

        // Load existing data
        loadPriceTable();
    }

    private void savePriceTable() {
        try {
            // Get first entry or create new one
            PriceTable priceTable = priceTableRepository.findAll().stream().findFirst().orElse(new PriceTable());

            priceTable.setMonthlyBasePackage(monthlyBasePackage.getValue());
            priceTable.setAppUsageLicense(appUsageLicense.getValue());
            priceTable.setRevenueParticipation(revenueParticipation.getValue());

            priceTableRepository.save(priceTable);
            Notification.show("Preise erfolgreich gespeichert!", 3000, Notification.Position.BOTTOM_CENTER);
        } catch (Exception ex) {
            Notification.show("Fehler beim Speichern: " + ex.getMessage(), 5000, Notification.Position.BOTTOM_CENTER);
        }
    }

    private void loadPriceTable() {
        try {
            PriceTable priceTable = priceTableRepository.findAll().stream().findFirst().orElse(null);

            if (priceTable != null) {
                monthlyBasePackage
                        .setValue(priceTable.getMonthlyBasePackage() != null ? priceTable.getMonthlyBasePackage() : "");
                appUsageLicense
                        .setValue(priceTable.getAppUsageLicense() != null ? priceTable.getAppUsageLicense() : "");
                revenueParticipation.setValue(
                        priceTable.getRevenueParticipation() != null ? priceTable.getRevenueParticipation() : "");
            }
        } catch (Exception ex) {
            Notification.show("Fehler beim Laden der Daten: " + ex.getMessage(), 5000,
                    Notification.Position.BOTTOM_CENTER);
        }
    }
}
