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.Pre;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

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

    public IntegrationView() {
        addClassName("content-view");
        add(
                createIntro(),
                createRepositoryCard(),
                createNextStepsCard()
        );
    }

    private Div createIntro() {
        Span eyebrow = new Span("Struktur");
        eyebrow.addClassName("eyebrow");

        H2 title = new H2("Das Repository ist jetzt fuer Mobile und Web vorbereitet.");
        title.addClassName("hero-title");

        Paragraph copy = new Paragraph(
                "Das Flutter-Projekt lebt jetzt in /app. Das Web-Backoffice ist bewusst entkoppelt in /backend angelegt, "
                        + "damit Build, Runtime und Deployment separat steuerbar bleiben.");
        copy.addClassName("hero-copy");

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

    private Div createRepositoryCard() {
        H3 title = new H3("Projektlayout");
        title.addClassName("card-title");

        Pre layout = new Pre("""
                HHA/
                |- app/
                |  |- lib/              Flutter-App
                |  |- android/          Flutter Android-Projekt
                |  `- pubspec.yaml
                |- backend/
                |  |- src/main/java/    Spring Boot + Vaadin
                |  |- src/main/resources/
                |  |- src/test/java/
                |  |- pom.xml
                |  |- mvnw
                |  `- .mvn/
                `- README.md
                """);
        layout.addClassName("repo-tree");

        Div card = new Div(title, layout);
        card.addClassName("wide-card");
        return card;
    }

    private Div createNextStepsCard() {
        H3 title = new H3("Naechste sinnvolle Schritte");
        title.addClassName("card-title");

        Paragraph first = new Paragraph("1. Fachliche REST-Endpunkte unter /api aufbauen und von Flutter konsumieren.");
        Paragraph second = new Paragraph("2. Vaadin-Views fuer Backoffice, Disposition oder Administration anlegen.");
        Paragraph third = new Paragraph("3. Build und Deployment fuer Mobile und Backend getrennt in CI/CD ausrollen.");

        Anchor statusLink = createExternalLink("/api/status", "Status-Endpunkt pruefen");
        Anchor healthLink = createExternalLink("/actuator/health", "Health-Check pruefen");

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

        Div card = new Div(title, first, second, third, actions);
        card.addClassName("wide-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;
    }
}
