package io.gmss.fiscad.entities.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.gmss.fiscad.entities.BaseEntity;
import io.gmss.fiscad.entities.decoupage.Secteur;
import io.gmss.fiscad.entities.infocad.metier.Enquete;
import io.gmss.fiscad.entities.infocad.parametre.Structure;
import io.gmss.fiscad.entities.rfu.parametre.Participer;
import io.gmss.fiscad.enums.UserRole;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SQLDelete(sql =
        "UPDATE users " +
                "SET deleted = true " +
                "WHERE id = ?")
@Where(clause = " deleted = false")
@Table(name = "users")
public class User extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nom;
    private String prenom;
    private String tel;
    private String email;
    private String username;
    @JsonIgnore
    private String password;
    @Column(columnDefinition = "boolean default true")
    private boolean active;
    @Column(columnDefinition = "boolean default false")
    private boolean resetPassword;
    @ManyToMany
    @JoinTable(name = "users_roles",
            joinColumns = @JoinColumn(name = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "roles_id")
    )
    private Set<Role> roles;
    @ManyToOne
    private Structure structure;
//    @JsonIgnore
//    @OneToMany(mappedBy = "user")
//    private List<Enquete> enquetes;

    @JsonIgnore
    @OneToMany(mappedBy = "user")
    private List<Participer> participers;

//    @JsonIgnore
//    @OneToMany(mappedBy = "chefSecteur")
//    private List<Secteur> secteurs;

    @Transient
    private Long idCampagneCourant;
    @Transient
    private Long idSecteurCourant;

    public void setUsername(String username) {
        this.username = this.email;
    }

    public boolean isAdmin() {
        for (Role r : this.roles) {
            if (r.getNom().equals(UserRole.ROLE_ADMIN)) {
                return true;
            }
        }
        return false;
    }


    public Long getIdCampagneCourant() {
        if(participers!=null) {
            for (Participer p : participers) {
                if (p.getDateFin() == null) {
                    if (p.getEquipe()!= null && p.getEquipe().getCampagne() != null) {
                        return p.getEquipe().getCampagne().getId();
                    }
                }
            }
        }
        return null;
    }

    public Long getIdSecteurCourant() {
        if(participers!=null){
        for (Participer p : participers) {
            if (p.getDateFin() == null) {
                if(p.getEquipe()!=null && p.getEquipe().getSecteur()!=null) {
                    return p.getEquipe().getSecteur().getId();
                }
            }
        }
        }
        return null ;
    }


}
