package dev.lions.unionflow.client.view;

import dev.lions.unionflow.client.service.PublicKpiRestClient;
import dev.lions.unionflow.server.api.dto.compliance.response.KpiPublicSnapshot;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.Serializable;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.logging.Logger;

/**
 * Bean public consultation KPI (Sprint 17) — pas d'authentification requise.
 *
 * <p>Lit le query param {@code token} dès l'init via {@code @ViewScoped} + viewAction,
 * appelle l'endpoint public, expose le snapshot à la page.
 *
 * @since 2026-04-25 (Sprint 17)
 */
@Named
@ViewScoped
public class PublicKpiBean implements Serializable {

  private static final long serialVersionUID = 1L;
  private static final Logger LOG = Logger.getLogger(PublicKpiBean.class);

  @Inject @RestClient PublicKpiRestClient client;

  private String token;
  private KpiPublicSnapshot snapshot;
  private String erreur;

  /** Appelé via {@code <f:viewAction action="#{publicKpiBean.charger}" />}. */
  public void charger() {
    erreur = null;
    snapshot = null;

    if (token == null || token.isBlank()) {
      // Token vient du query param via <f:viewParam>
      token = FacesContext.getCurrentInstance()
          .getExternalContext()
          .getRequestParameterMap()
          .get("token");
    }

    if (token == null || token.isBlank()) {
      erreur = "Aucun token fourni dans l'URL.";
      return;
    }

    try {
      snapshot = client.consulter(token);
      LOG.infof("PublicKpi chargé — score=%d", snapshot == null ? -1 : snapshot.scoreGlobal());
    } catch (jakarta.ws.rs.WebApplicationException wae) {
      int status = wae.getResponse().getStatus();
      if (status == 401) {
        erreur = "Token invalide ou expiré.";
      } else if (status == 404) {
        erreur = "Organisation introuvable.";
      } else {
        erreur = "Erreur HTTP " + status;
      }
      LOG.warnf("PublicKpi HTTP %d : %s", status, wae.getMessage());
    } catch (Exception e) {
      erreur = "Erreur de chargement : " + e.getMessage();
      LOG.errorf(e, "PublicKpi exception");
    }
  }

  /** Couleur sémantique du score (success / warning / danger). */
  public String getCouleurScore() {
    if (snapshot == null) return "secondary";
    int s = snapshot.scoreGlobal();
    if (s >= 80) return "success";
    if (s >= 60) return "warning";
    return "danger";
  }

  public String getCouleurStatut(String statut) {
    if (statut == null) return "secondary";
    return switch (statut) {
      case "OK" -> "success";
      case "RETARD" -> "danger";
      case "EN_ATTENTE" -> "warning";
      case "OBLIGATOIRE" -> "info";
      default -> "secondary";
    };
  }

  // Getters / setters
  public String getToken() { return token; }
  public void setToken(String token) { this.token = token; }
  public KpiPublicSnapshot getSnapshot() { return snapshot; }
  public String getErreur() { return erreur; }
  public boolean isCharge() { return snapshot != null; }
}
