package dev.lions.unionflow.server.api.dto.formuleabonnement.response;

import dev.lions.unionflow.server.api.dto.base.BaseResponse;
import dev.lions.unionflow.server.api.enums.formuleabonnement.StatutFormule;
import dev.lions.unionflow.server.api.enums.formuleabonnement.TypeFormule;
import java.math.BigDecimal;
import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * Réponse détaillée pour une formule d'abonnement.
 */
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FormuleAbonnementResponse extends BaseResponse {

    private String nom;
    private String code;
    private String description;
    private TypeFormule type;
    private StatutFormule statut;
    private BigDecimal prixMensuel;
    private BigDecimal prixAnnuel;
    @Builder.Default
    private String devise = "XOF";
    private Integer maxMembres;
    private Integer maxAdministrateurs;
    private BigDecimal espaceStockageGB;
    private Boolean supportTechnique;
    private String niveauSupport;
    private Boolean fonctionnalitesAvancees;
    private Boolean apiAccess;
    private Boolean rapportsPersonnalises;
    private Boolean integrationsTierces;
    private Boolean sauvegardeAutomatique;
    private Boolean multiLangues;
    private Boolean personnalisationInterface;
    private Boolean formationIncluse;
    private Integer heuresFormation;
    private Boolean populaire;
    private Boolean recommandee;
    private Integer periodeEssaiJours;
    private LocalDate dateDebutValidite;
    private LocalDate dateFinValidite;
    private Integer ordreAffichage;
    private String couleur;
    private String icone;
    private String notes;

    public boolean isActive() {
        return StatutFormule.ACTIVE.equals(statut);
    }

    public boolean isInactive() {
        return StatutFormule.INACTIVE.equals(statut);
    }

    public boolean isArchivee() {
        return StatutFormule.ARCHIVEE.equals(statut);
    }

    public boolean isValide() {
        if (!isActive())
            return false;

        LocalDate aujourd = LocalDate.now();
        if (dateDebutValidite != null && aujourd.isBefore(dateDebutValidite))
            return false;
        if (dateFinValidite != null && aujourd.isAfter(dateFinValidite))
            return false;
        return true;
    }

    public BigDecimal getEconomieAnnuelle() {
        if (prixMensuel == null || prixAnnuel == null)
            return BigDecimal.ZERO;
        BigDecimal coutMensuelAnnuel = prixMensuel.multiply(BigDecimal.valueOf(12));
        return coutMensuelAnnuel.subtract(prixAnnuel);
    }

    public int getPourcentageEconomieAnnuelle() {
        if (prixMensuel == null || prixAnnuel == null)
            return 0;
        BigDecimal coutMensuelAnnuel = prixMensuel.multiply(BigDecimal.valueOf(12));
        if (coutMensuelAnnuel.compareTo(BigDecimal.ZERO) > 0) {
            return getEconomieAnnuelle()
                    .multiply(BigDecimal.valueOf(100))
                    .divide(coutMensuelAnnuel, 0, java.math.RoundingMode.HALF_UP)
                    .intValue();
        }
        return 0;
    }

    public boolean hasPeriodeEssai() {
        return periodeEssaiJours != null && periodeEssaiJours > 0;
    }

    public boolean hasFormation() {
        return Boolean.TRUE.equals(formationIncluse) && heuresFormation != null && heuresFormation > 0;
    }

    public boolean isMiseEnAvant() {
        return Boolean.TRUE.equals(populaire) || Boolean.TRUE.equals(recommandee);
    }

    public String getBadge() {
        if (Boolean.TRUE.equals(populaire))
            return "POPULAIRE";
        if (Boolean.TRUE.equals(recommandee))
            return "RECOMMANDÉE";
        if (hasPeriodeEssai())
            return "ESSAI GRATUIT";
        return null;
    }

    public int getScoreFonctionnalites() {
        if (supportTechnique == null && sauvegardeAutomatique == null && fonctionnalitesAvancees == null
                && apiAccess == null && rapportsPersonnalises == null && integrationsTierces == null
                && multiLangues == null && personnalisationInterface == null) {
            return 0;
        }
        int score = 0;
        int total = 0;
        if (Boolean.TRUE.equals(supportTechnique)) score += 10;
        total += 10;
        if (Boolean.TRUE.equals(sauvegardeAutomatique)) score += 10;
        total += 10;
        if (Boolean.TRUE.equals(fonctionnalitesAvancees)) score += 15;
        total += 15;
        if (Boolean.TRUE.equals(apiAccess)) score += 15;
        total += 15;
        if (Boolean.TRUE.equals(rapportsPersonnalises)) score += 15;
        total += 15;
        if (Boolean.TRUE.equals(integrationsTierces)) score += 15;
        total += 15;
        if (Boolean.TRUE.equals(multiLangues)) score += 10;
        total += 10;
        if (Boolean.TRUE.equals(personnalisationInterface)) score += 10;
        total += 10;
        return (score * 100) / total;
    }

    public String getCssClass() {
        if (type == null)
            return "formule-default";
        return switch (type) {
            case BASIC -> "formule-basic";
            case STANDARD -> "formule-standard";
            case PREMIUM -> "formule-premium";
            case ENTERPRISE -> "formule-enterprise";
        };
    }
}
