package de.assecutor.votianlt.model;

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

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(collection = "pending_mqtt_messages")
public class PendingMqttMessage {

    @Id
    private ObjectId id;

    @Field("topic")
    private String topic;

    @Field("payload")
    private byte[] payload;

    @Field("qos")
    private int qos;

    @Field("retained")
    private boolean retained;

    @Field("created_at")
    private LocalDateTime createdAt;

    @Field("retry_count")
    private int retryCount = 0;

    @Field("last_retry_at")
    private LocalDateTime lastRetryAt;

    public PendingMqttMessage(String topic, byte[] payload, int qos, boolean retained) {
        this.topic = topic;
        this.payload = payload;
        this.qos = qos;
        this.retained = retained;
        this.createdAt = LocalDateTime.now();
        this.retryCount = 0;
    }

    public void incrementRetryCount() {
        this.retryCount++;
        this.lastRetryAt = LocalDateTime.now();
    }
}