package com.gbcm.client.resources;

import io.quarkus.qute.Template;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.oidc.OidcSession;
import io.quarkus.oidc.IdToken;
import org.eclipse.microprofile.jwt.JsonWebToken;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/pages")
public class PageController {

    @Inject
    Template dashboard;

    @Inject
    Template clients;

    @Inject
    Template profile;

    @Inject
    SecurityIdentity securityIdentity;

    @Inject
    OidcSession oidcSession;

    @Inject
    @IdToken
    JsonWebToken idToken;

    @GET
    @Path("/dashboard")
    @Produces(MediaType.TEXT_HTML)
    public TemplateInstance dashboard() {
        return dashboard
                .data("user", getCurrentUser())
                .data("authenticated", isAuthenticated())
                .data("title", "Tableau de Bord - GBCM");
    }

    @GET
    @Path("/clients")
    @Produces(MediaType.TEXT_HTML)
    public TemplateInstance clients() {
        return clients
                .data("user", getCurrentUser())
                .data("authenticated", isAuthenticated())
                .data("title", "Gestion des Clients - GBCM");
    }

    @GET
    @Path("/profile")
    @Produces(MediaType.TEXT_HTML)
    public TemplateInstance profile() {
        return profile
                .data("user", getCurrentUser())
                .data("authenticated", isAuthenticated())
                .data("title", "Profil Utilisateur - GBCM")
                .data("tokenInfo", getTokenInfo());
    }

    private boolean isAuthenticated() {
        return securityIdentity != null && !securityIdentity.isAnonymous();
    }

    private UserInfo getCurrentUser() {
        if (!isAuthenticated()) {
            return new UserInfo("Invité", "guest@example.com", "GUEST");
        }

        String username = securityIdentity.getPrincipal().getName();
        String email = idToken != null ? idToken.getClaim("email") : username;
        String role = securityIdentity.getRoles().isEmpty() ? "USER" : 
                     securityIdentity.getRoles().iterator().next();

        return new UserInfo(username, email, role);
    }

    private String getTokenInfo() {
        if (idToken == null) {
            return "Aucun token disponible";
        }

        StringBuilder info = new StringBuilder();
        info.append("Issuer: ").append(idToken.getIssuer()).append("\n");
        info.append("Subject: ").append(idToken.getSubject()).append("\n");
        info.append("Expiration: ").append(idToken.getExpirationTime()).append("\n");
        info.append("Issued At: ").append(idToken.getIssuedAtTime()).append("\n");

        return info.toString();
    }

    public static class UserInfo {
        public final String name;
        public final String email;
        public final String role;

        public UserInfo(String name, String email, String role) {
            this.name = name;
            this.email = email;
            this.role = role;
        }
    }
}
