package com.social.media.infrastructure.persistence.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

/**
 * Campaign Execution JPA Entity
 * Maps to the campaign_executions table
 */
@Entity
@Table(name = "campaign_executions", schema = "automation")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CampaignExecutionEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "execution_code", nullable = false, unique = true, length = 30)
    private String executionCode;
    
    @Column(name = "campaign_id", nullable = false)
    private Long campaignId;
    
    @Column(name = "action_type", nullable = false, length = 50)
    private String actionType;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, columnDefinition = "automation.execution_status")
    private ExecutionStatusEntity status;
    
    @Column(name = "target_user_id")
    private String targetUserId;
    
    @Column(name = "target_post_id")
    private String targetPostId;
    
    @Column(name = "target_comment_id")
    private String targetCommentId;
    
    @Column(name = "action_params", columnDefinition = "jsonb")
    private String actionParams;
    
    @Column(name = "result_data", columnDefinition = "jsonb")
    private String resultData;
    
    @Column(name = "planned_execution_date")
    private LocalDateTime plannedExecutionDate;
    
    @Column(name = "actual_execution_date")
    private LocalDateTime actualExecutionDate;
    
    @Column(name = "error_message")
    private String errorMessage;
    
    @Column(name = "retry_count")
    @Builder.Default
    private Integer retryCount = 0;
    
    @Column(name = "max_retries")
    @Builder.Default
    private Integer maxRetries = 3;
    
    @Column(name = "execution_duration_ms")
    private Long executionDurationMs;
    
    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    public enum ExecutionStatusEntity {
        PENDING, IN_PROGRESS, COMPLETED, FAILED, SKIPPED, CANCELLED
    }
}
