package de.assecutor.votianlt.model;

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.bson.types.ObjectId;

import java.time.LocalDateTime;

/**
 * Job History entity for tracking all changes made to a job. Each entry
 * represents a single change or action performed on a job.
 */
@Data
@Document(collection = "job_history")
public class JobHistory {

    @Id
    private ObjectId id;

    /**
     * Reference to the job this history entry belongs to
     */
    private ObjectId jobId;

    /**
     * Timestamp when the change occurred
     */
    private LocalDateTime timestamp;

    /**
     * Reason for the change (e.g., "Status Update", "User Edit", "System Update")
     */
    private String reason;

    /**
     * Description of what was changed (e.g., "Status changed from CREATED to
     * IN_PROGRESS")
     */
    private String description;

    /**
     * User who made the change (can be null for system changes)
     */
    private String changedBy;

    /**
     * Additional details about the change (optional)
     */
    private String details;

    /**
     * Type of change (CREATE, UPDATE, STATUS_CHANGE, DELETE, etc.)
     */
    private JobHistoryType changeType;

    /**
     * Old value (for comparison, stored as JSON string if complex)
     */
    private String oldValue;

    /**
     * New value (for comparison, stored as JSON string if complex)
     */
    private String newValue;

    // Default constructor
    public JobHistory() {
        this.timestamp = LocalDateTime.now();
    }

    // Constructor for basic history entry
    public JobHistory(ObjectId jobId, String reason, String description, String changedBy) {
        this();
        this.jobId = jobId;
        this.reason = reason;
        this.description = description;
        this.changedBy = changedBy;
    }

    // Constructor for detailed history entry
    public JobHistory(ObjectId jobId, String reason, String description, String changedBy, JobHistoryType changeType,
            String oldValue, String newValue) {
        this(jobId, reason, description, changedBy);
        this.changeType = changeType;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    // Getter for ID as String
    public String getIdAsString() {
        return id != null ? id.toHexString() : null;
    }

    // Getter for Job ID as String
    public String getJobIdAsString() {
        return jobId != null ? jobId.toHexString() : null;
    }
}