package de.assecutor.hha.backend.views;

import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.H3;
import com.vaadin.flow.component.html.Paragraph;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

@PageTitle("Dashboard | HHA Backoffice")
@Route(value = "", layout = MainLayout.class)
public class DashboardView extends Div {

    public DashboardView() {
        addClassName("content-view");
        add(
                createHero(),
                createSectionTitle("Module im Repository"),
                createModuleGrid(),
                createSectionTitle("Direkt verfuegbare Endpunkte"),
                createEndpointGrid()
        );
    }

    private Div createHero() {
        Span eyebrow = new Span("Startpunkt");
        eyebrow.addClassName("eyebrow");

        H2 title = new H2("Vaadin-Weboberflaeche und Spring-Boot-Backend sind jetzt Teil desselben Repositories.");
        title.addClassName("hero-title");

        Paragraph copy = new Paragraph(
                "Das Web-Modul lebt isoliert unter /backend, kann separat gebaut werden und bildet die Grundlage fuer "
                        + "ein Backoffice, Monitoring-Ansichten oder spaetere APIs fuer die Flutter-App in /app.");
        copy.addClassName("hero-copy");

        Anchor apiLink = createExternalLink("/api/status", "API Status");
        Anchor healthLink = createExternalLink("/actuator/health", "Actuator Health");

        Div actions = new Div(apiLink, healthLink);
        actions.addClassName("action-row");

        Div hero = new Div(eyebrow, title, copy, actions);
        hero.addClassName("hero-card");
        return hero;
    }

    private Div createModuleGrid() {
        Div grid = new Div(
                createInfoCard("Flutter App", "Mobile Client",
                        "Die bestehende Flutter-Anwendung liegt jetzt gekapselt im Modul /app."),
                createInfoCard("Spring Boot", "Backend Runtime",
                        "Boot 4.0.3 liefert den Server, REST-Endpunkte und die Anwendungskonfiguration."),
                createInfoCard("Vaadin", "Web UI",
                        "Vaadin 25.0.7 stellt die serverseitige Admin- und Backoffice-Oberflaeche bereit.")
        );
        grid.addClassName("card-grid");
        return grid;
    }

    private Div createEndpointGrid() {
        Div grid = new Div(
                createEndpointCard("/api/status", "JSON-Status fuer externe Clients"),
                createEndpointCard("/actuator/health", "Health-Endpunkt fuer Betrieb und Monitoring"),
                createEndpointCard("/", "Vaadin-Startseite fuer das Web-Backoffice")
        );
        grid.addClassName("card-grid");
        return grid;
    }

    private Div createSectionTitle(String text) {
        H3 title = new H3(text);
        title.addClassName("section-title");

        Div wrapper = new Div(title);
        wrapper.addClassName("section-header");
        return wrapper;
    }

    private Div createInfoCard(String label, String title, String body) {
        Span chip = new Span(label);
        chip.addClassName("pill");

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

        Paragraph copy = new Paragraph(body);
        copy.addClassName("card-copy");

        Div card = new Div(chip, heading, copy);
        card.addClassName("info-card");
        return card;
    }

    private Div createEndpointCard(String path, String description) {
        Anchor link = createExternalLink(path, path);
        link.addClassName("endpoint-link");

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

        Div card = new Div(link, copy);
        card.addClassName("info-card");
        return card;
    }

    private Anchor createExternalLink(String href, String text) {
        Anchor anchor = new Anchor(href, text);
        anchor.getElement().setAttribute("router-ignore", true);
        anchor.setTarget("_blank");
        anchor.addClassName("action-link");
        return anchor;
    }
}
