package com.social.media.infrastructure.persistence.campaign;

import com.social.media.domain.campaign.valueobject.CampaignInteractionStatus;
import com.social.media.domain.campaign.valueobject.CampaignInteractionType;
import jakarta.persistence.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;

/**
 * JPA Entity for Campaign
 */
@Entity
@Table(name = "campaigns", indexes = {
    @Index(name = "idx_campaigns_user_id", columnList = "user_id"),
    @Index(name = "idx_campaigns_bot_id", columnList = "bot_id"),
    @Index(name = "idx_campaigns_account_network_id", columnList = "account_network_id"),
    @Index(name = "idx_campaigns_status_interation", columnList = "status_interation"),
    @Index(name = "idx_campaigns_type_interation", columnList = "type_interation"),
    @Index(name = "idx_campaigns_date_interation", columnList = "date_interation"),
    @Index(name = "idx_campaigns_date_start", columnList = "date_start"),
    @Index(name = "idx_campaigns_date_end", columnList = "date_end"),
    @Index(name = "idx_campaigns_user_id_name", columnList = "user_id, name", unique = true)
})
public class CampaignJpaEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "name", nullable = false, length = 50)
    private String name;
    
    @Column(name = "description", length = 255)
    private String description;
    
    @Column(name = "user_id", nullable = false)
    private Long userId;
    
    @Column(name = "company_id", nullable = false)
    private Long companyId;
    
    @Column(name = "account_network_id", nullable = false)
    private Long accountNetworkId;
    
    @Column(name = "bot_id", nullable = false)
    private Long botId;
    
    @Column(name = "script_id")
    private Long scriptId;
    
    @Column(name = "list_id")
    private Long listId;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "type_interation", nullable = false)
    private CampaignInteractionType typeInteration;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "status_interation", nullable = false)
    private CampaignInteractionStatus statusInteration = CampaignInteractionStatus.WAIT;
    
    @Column(name = "date_interation")
    private LocalDate dateInteration;
    
    @Column(name = "hour_interation")
    private LocalTime hourInteration;
    
    @Column(name = "text_interation", columnDefinition = "TEXT")
    private String textInteration;
    
    @Column(name = "date_start")
    private LocalDate dateStart;
    
    @Column(name = "hour_start")
    private LocalTime hourStart;
    
    @Column(name = "date_end")
    private LocalDate dateEnd;
    
    @Column(name = "hour_end")
    private LocalTime hourEnd;
    
    // Audit fields
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    @Version
    @Column(name = "version")
    private Long version = 0L;
    
    // Constructors
    public CampaignJpaEntity() {}
    
    public CampaignJpaEntity(String name, String description, Long userId, Long companyId,
                           Long accountNetworkId, Long botId, 
                           CampaignInteractionType typeInteration) {
        this.name = name;
        this.description = description;
        this.userId = userId;
        this.companyId = companyId;
        this.accountNetworkId = accountNetworkId;
        this.botId = botId;
        this.typeInteration = typeInteration;
        this.statusInteration = CampaignInteractionStatus.WAIT;
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    @PrePersist
    protected void onCreate() {
        if (createdAt == null) {
            createdAt = LocalDateTime.now();
        }
        if (updatedAt == null) {
            updatedAt = LocalDateTime.now();
        }
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    // Getters and Setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = 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 Long getUserId() { return userId; }
    public void setUserId(Long userId) { this.userId = userId; }
    
    public Long getCompanyId() { return companyId; }
    public void setCompanyId(Long companyId) { this.companyId = companyId; }
    
    public Long getAccountNetworkId() { return accountNetworkId; }
    public void setAccountNetworkId(Long accountNetworkId) { this.accountNetworkId = accountNetworkId; }
    
    public Long getBotId() { return botId; }
    public void setBotId(Long botId) { this.botId = botId; }
    
    public Long getScriptId() { return scriptId; }
    public void setScriptId(Long scriptId) { this.scriptId = scriptId; }
    
    public Long getListId() { return listId; }
    public void setListId(Long listId) { this.listId = listId; }
    
    public CampaignInteractionType getTypeInteration() { return typeInteration; }
    public void setTypeInteration(CampaignInteractionType typeInteration) { this.typeInteration = typeInteration; }
    
    public CampaignInteractionStatus getStatusInteration() { return statusInteration; }
    public void setStatusInteration(CampaignInteractionStatus statusInteration) { this.statusInteration = statusInteration; }
    
    public LocalDate getDateInteration() { return dateInteration; }
    public void setDateInteration(LocalDate dateInteration) { this.dateInteration = dateInteration; }
    
    public LocalTime getHourInteration() { return hourInteration; }
    public void setHourInteration(LocalTime hourInteration) { this.hourInteration = hourInteration; }
    
    public String getTextInteration() { return textInteration; }
    public void setTextInteration(String textInteration) { this.textInteration = textInteration; }
    
    public LocalDate getDateStart() { return dateStart; }
    public void setDateStart(LocalDate dateStart) { this.dateStart = dateStart; }
    
    public LocalTime getHourStart() { return hourStart; }
    public void setHourStart(LocalTime hourStart) { this.hourStart = hourStart; }
    
    public LocalDate getDateEnd() { return dateEnd; }
    public void setDateEnd(LocalDate dateEnd) { this.dateEnd = dateEnd; }
    
    public LocalTime getHourEnd() { return hourEnd; }
    public void setHourEnd(LocalTime hourEnd) { this.hourEnd = hourEnd; }
    
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    
    public LocalDateTime getUpdatedAt() { return updatedAt; }
    public void setUpdatedAt(LocalDateTime updatedAt) { this.updatedAt = updatedAt; }
    
    public Long getVersion() { return version; }
    public void setVersion(Long version) { this.version = version; }
}
