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

import dev.lions.unionflow.server.api.dto.base.BaseResponse;
import dev.lions.unionflow.server.api.dto.solidarite.BeneficiaireAideDTO;
import dev.lions.unionflow.server.api.dto.solidarite.response.CommentaireAideResponse;
import dev.lions.unionflow.server.api.dto.solidarite.ContactUrgenceDTO;
import dev.lions.unionflow.server.api.dto.solidarite.HistoriqueStatutDTO;
import dev.lions.unionflow.server.api.dto.solidarite.LocalisationDTO;
import dev.lions.unionflow.server.api.dto.solidarite.PieceJustificativeDTO;
import dev.lions.unionflow.server.api.enums.solidarite.PrioriteAide;
import dev.lions.unionflow.server.api.enums.solidarite.StatutAide;
import dev.lions.unionflow.server.api.enums.solidarite.TypeAide;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
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 demande d'aide.
 */
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DemandeAideResponse extends BaseResponse {

    private String numeroReference;
    private TypeAide typeAide;
    private String titre;
    private String description;
    private String justification;

    private BigDecimal montantDemande;
    private BigDecimal montantApprouve;
    private BigDecimal montantVerse;
    private String devise;

    private UUID membreDemandeurId;
    private String nomDemandeur;
    private String numeroMembreDemandeur;

    private String evaluateurId;
    private String evaluateurNom;
    private String approvateurId;
    private String approvateurNom;

    private UUID associationId;
    private String nomAssociation;

    private StatutAide statut;
    private PrioriteAide priorite;

    private String motifRejet;
    private String commentairesEvaluateur;

    private LocalDateTime dateSoumission;
    private LocalDateTime dateLimiteTraitement;
    private LocalDateTime dateEvaluation;
    private LocalDateTime dateApprobation;
    private LocalDateTime dateVersement;
    private LocalDateTime dateCloture;

    private List<PieceJustificativeDTO> piecesJustificatives;
    private List<BeneficiaireAideDTO> beneficiaires;
    private List<HistoriqueStatutDTO> historiqueStatuts;
    private List<CommentaireAideResponse> commentaires;
    private Map<String, Object> donneesPersonnalisees;
    private List<String> tags;

    private Boolean estConfidentielle;
    private Boolean necessiteSuivi;
    private Double scorePriorite;
    private Integer nombreVues;

    private LocalisationDTO localisation;
    private ContactUrgenceDTO contactUrgence;

    private LocalDate dateLimite;
    private Boolean justificatifsFournis;
    private String documentsJoints;
    private LocalDate dateDebutAide;
    private LocalDate dateFinAide;

    private UUID membreAidantId;
    private String nomAidant;
    private String modeVersement;
    private String numeroTransaction;

    private UUID rejeteParId;
    private String rejetePar;
    private LocalDateTime dateRejet;
    private String raisonRejet;

    // === MÉTHODES UTILITAIRES ===

    public boolean estModifiable() {
        return statut != null && statut.permetModification();
    }

    public boolean peutEtreAnnulee() {
        return statut != null && statut.permetAnnulation();
    }

    public boolean estUrgente() {
        return priorite != null && priorite.isUrgente();
    }

    public boolean estTerminee() {
        return statut != null && statut.isEstFinal();
    }

    public boolean estEnSucces() {
        return statut != null && statut.isSucces();
    }

    public double getPourcentageAvancement() {
        if (statut == null) {
            return 0.0;
        }

        return switch (statut) {
            case BROUILLON -> 5.0;
            case SOUMISE -> 10.0;
            case EN_ATTENTE -> 20.0;
            case EN_COURS_EVALUATION -> 40.0;
            case INFORMATIONS_REQUISES -> 35.0;
            case APPROUVEE, APPROUVEE_PARTIELLEMENT -> 60.0;
            case EN_COURS_TRAITEMENT -> 70.0;
            case EN_COURS_VERSEMENT -> 85.0;
            case VERSEE, LIVREE, TERMINEE -> 100.0;
            case REJETEE, ANNULEE, EXPIREE -> 100.0;
            case SUSPENDUE -> 50.0;
            case EN_SUIVI -> 95.0;
            case CLOTUREE -> 100.0;
        };
    }

    public long getDelaiRestantHeures() {
        if (dateLimiteTraitement == null) {
            return -1;
        }

        LocalDateTime maintenant = LocalDateTime.now();
        if (maintenant.isAfter(dateLimiteTraitement)) {
            return 0;
        }

        return java.time.Duration.between(maintenant, dateLimiteTraitement).toHours();
    }

    public boolean estDelaiDepasse() {
        return getDelaiRestantHeures() == 0;
    }

    public String getStatutLibelle() {
        return statut != null ? statut.getLibelle() : "Non défini";
    }

    public String getPrioriteLibelle() {
        return priorite != null ? priorite.getLibelle() : "Normale";
    }
}
