package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import de.assecutor.votianlt.model.AppUser;
import de.assecutor.votianlt.pages.service.AppUserService;
import jakarta.annotation.security.RolesAllowed;
import org.springframework.beans.factory.annotation.Autowired;

@PageTitle("App-Nutzer")
@Route(value = "app-user", layout = de.assecutor.votianlt.pages.base.ui.view.MainLayout.class)
@RolesAllowed({ "USER", "ADMIN" })
public class AppUserView extends VerticalLayout {

    private final AppUserService appUserService;
    private final Grid<AppUser> appUserGrid;

    @Autowired
    public AppUserView(AppUserService appUserService) {
        this.appUserService = appUserService;

        setSizeFull();
        setPadding(true);
        setSpacing(true);

        // Header mit Titel und Button
        HorizontalLayout header = new HorizontalLayout();
        header.setWidthFull();
        header.setAlignItems(FlexComponent.Alignment.CENTER);

        H2 title = new H2("App-Nutzer");
        title.getStyle().set("margin", "0");

        Button addButton = new Button("Neuen App-Nutzer anlegen", new Icon(VaadinIcon.PLUS));
        addButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
        addButton.addClickListener(e -> navigateToAddAppUser());

        header.add(title, addButton);
        header.setJustifyContentMode(FlexComponent.JustifyContentMode.BETWEEN);
        add(header);

        // Grid für App-Nutzer
        appUserGrid = new Grid<>(AppUser.class, false);
        appUserGrid.setSizeFull();

        // Grid-Spalten konfigurieren
        appUserGrid.addColumn(AppUser::getBezeichnung).setHeader("Bezeichnung").setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getVorname).setHeader("Vorname").setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getNachname).setHeader("Nachname").setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getTelefon).setHeader("Telefon").setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getAppCode).setHeader("App-Code").setAutoWidth(true);
        appUserGrid.addColumn(AppUser::getEmail).setHeader("E-Mail").setAutoWidth(true);

        // Make grid rows clickable
        appUserGrid.setSelectionMode(Grid.SelectionMode.SINGLE);
        appUserGrid.getStyle().set("cursor", "pointer");

        // Add click listener to navigate to edit view
        appUserGrid.addItemClickListener(event -> {
            AppUser appUser = event.getItem();
            if (appUser != null && appUser.getId() != null) {
                getUI().ifPresent(ui -> ui.navigate("edit-app-user/" + appUser.getId().toHexString()));
            }
        });

        add(appUserGrid);

        // Daten laden
        loadAppUsers();
    }

    private void loadAppUsers() {
        var appUsers = appUserService.findByCurrentUser();
        appUserGrid.setItems(appUsers);
    }

    private void navigateToAddAppUser() {
        getUI().ifPresent(ui -> ui.navigate("add-app-user"));
    }
}
