package de.assecutor.votianlt.pages.view;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.html.*;
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.VerticalLayout;
import com.vaadin.flow.router.HasDynamicTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLink;
import de.assecutor.votianlt.model.User;
import de.assecutor.votianlt.security.SecurityService;
import de.assecutor.votianlt.pages.base.ui.view.MainLayout;
import jakarta.annotation.security.RolesAllowed;

@Route(value = "dashboard", layout = MainLayout.class)
@RolesAllowed({ "USER" })
public class AuthenticatedStartView extends VerticalLayout implements HasDynamicTitle {

    private final SecurityService securityService;

    public AuthenticatedStartView(SecurityService securityService) {
        this.securityService = securityService;

        // Redirect admin users to admin dashboard
        if (securityService.hasRole("ADMIN")) {
            getUI().ifPresent(ui -> ui.navigate("admin-dashboard"));
            return;
        }

        setSizeFull();
        setPadding(false);
        setSpacing(true);
        setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.STRETCH);
        addClassName("dashboard-home-view");

        // Hero Section for authenticated users
        add(createAuthenticatedHeroSection());

        // System Section
        add(createSystemSection());
    }

    private Component createAuthenticatedHeroSection() {
        VerticalLayout heroSection = new VerticalLayout();
        heroSection.setWidthFull();
        heroSection.setPadding(true);
        heroSection.setSpacing(true);
        heroSection.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        heroSection.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
        heroSection.addClassNames("page-shell", "hero-panel", "landing-hero");

        // Welcome message for authenticated user
        User currentUser = securityService.getCurrentDatabaseUser();
        String displayName = currentUser != null && currentUser.getFirstname() != null && currentUser.getName() != null
                ? currentUser.getFirstname() + " " + currentUser.getName()
                : securityService.getCurrentUsername();
        H1 welcomeTitle = new H1(getTranslation("dashboard.welcome", displayName));
        welcomeTitle.addClassName("hero-panel-title");

        Paragraph welcomeDescription = new Paragraph(getTranslation("dashboard.description"));
        welcomeDescription.addClassName("hero-panel-text");

        heroSection.add(welcomeTitle, welcomeDescription);
        return heroSection;
    }

    private Component createSystemSection() {
        VerticalLayout systemSection = new VerticalLayout();
        systemSection.setWidthFull();
        systemSection.setPadding(true);
        systemSection.setSpacing(true);
        systemSection.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        systemSection.addClassNames("page-shell", "surface-panel", "section-panel");

        // Section Header
        H2 systemTitle = new H2(getTranslation("dashboard.system.title"));
        systemTitle.addClassName("section-title");

        Paragraph systemIntro = new Paragraph(getTranslation("dashboard.system.intro"));
        systemIntro.addClassName("section-intro");

        // Features Grid - using Div with CSS flexbox for proper centering
        Div featuresGrid = new Div();
        featuresGrid.addClassName("tile-grid");

        // Feature Cards
        featuresGrid.add(
                createFeatureCard(VaadinIcon.COG, getTranslation("dashboard.feature.setup.title"),
                        getTranslation("dashboard.feature.setup.desc"), EditProfileView.class),
                createFeatureCard(VaadinIcon.USERS, getTranslation("dashboard.feature.customers.title"),
                        getTranslation("dashboard.feature.customers.desc"), ShowCustomersView.class),
                createFeatureCard(VaadinIcon.CLIPBOARD_TEXT, getTranslation("dashboard.feature.jobs.title"),
                        getTranslation("dashboard.feature.jobs.desc"), ShowJobsView.class));

        systemSection.add(systemTitle, systemIntro, featuresGrid);
        return systemSection;
    }

    private Component createFeatureCard(VaadinIcon iconType, String title, String description,
            Class<? extends Component> navigationTarget) {
        VerticalLayout card = new VerticalLayout();
        card.setDefaultHorizontalComponentAlignment(FlexComponent.Alignment.CENTER);
        card.setPadding(true);
        card.setSpacing(true);
        card.setWidthFull();
        card.addClassName("feature-card");

        Icon icon = iconType.create();
        icon.setSize("48px");
        icon.addClassName("feature-card-icon");

        H3 cardTitle = new H3(title);
        cardTitle.addClassName("feature-card-title");

        Paragraph cardDescription = new Paragraph(description);
        cardDescription.addClassName("feature-card-description");

        card.add(icon, cardTitle, cardDescription);

        RouterLink link = new RouterLink();
        link.setRoute(navigationTarget);
        link.add(card);
        link.addClassName("feature-card-link");
        link.getStyle().set("width", "100%");
        link.getElement().setAttribute("aria-label", title);
        return link;
    }

    @Override
    public String getPageTitle() {
        return getTranslation("page.title.dashboard");
    }
}
