package pl.polskalokalnie.lead;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * Grupa docelowa jako dynamiczny segment - przechowuje zestaw kryteriow filtra, a nie zamrozona
 * liste leadow. Czlonkowie wyliczaja sie na biezaco w {@link LeadQueryService} przy podgladzie
 * i przy starcie kampanii. Puste kryterium oznacza brak ograniczenia dla danego pola.
 */
@Entity
@Table(name = "target_groups")
public class TargetGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 140)
    private String name;

    @Column(length = 400)
    private String description;

    // EAGER: zbior zrodel jest maly i zawsze uzywany przy wyliczaniu segmentu oraz w DTO
    // (mapowanie poza sesja - open-in-view=false).
    @ElementCollection(fetch = jakarta.persistence.FetchType.EAGER)
    @CollectionTable(name = "target_group_sources", joinColumns = @JoinColumn(name = "group_id"))
    @Column(name = "source", length = 20)
    @Enumerated(EnumType.STRING)
    private Set<LeadSource> sources = new LinkedHashSet<>();

    @Column(length = 120)
    private String city;

    @Column(length = 60)
    private String tag;

    @Enumerated(EnumType.STRING)
    @Column(length = 20)
    private LeadStatus status;

    @Column(nullable = false)
    private boolean requireEmail = false;

    @Column(nullable = false)
    private boolean requirePhone = false;

    @Column(nullable = false)
    private boolean requireEmailConsent = false;

    @Column(nullable = false)
    private boolean requireSmsConsent = false;

    @Column(nullable = false, updatable = false)
    private Instant createdAt;

    @PrePersist
    void onCreate() {
        if (createdAt == null) {
            createdAt = Instant.now();
        }
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<LeadSource> getSources() {
        return sources;
    }

    public void setSources(Set<LeadSource> sources) {
        this.sources = sources;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public LeadStatus getStatus() {
        return status;
    }

    public void setStatus(LeadStatus status) {
        this.status = status;
    }

    public boolean isRequireEmail() {
        return requireEmail;
    }

    public void setRequireEmail(boolean requireEmail) {
        this.requireEmail = requireEmail;
    }

    public boolean isRequirePhone() {
        return requirePhone;
    }

    public void setRequirePhone(boolean requirePhone) {
        this.requirePhone = requirePhone;
    }

    public boolean isRequireEmailConsent() {
        return requireEmailConsent;
    }

    public void setRequireEmailConsent(boolean requireEmailConsent) {
        this.requireEmailConsent = requireEmailConsent;
    }

    public boolean isRequireSmsConsent() {
        return requireSmsConsent;
    }

    public void setRequireSmsConsent(boolean requireSmsConsent) {
        this.requireSmsConsent = requireSmsConsent;
    }

    public Instant getCreatedAt() {
        return createdAt;
    }
}
