package dev.lions.dtos;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.lions.models.Notification;
import dev.lions.models.NotificationStatus;
import dev.lions.models.NotificationType;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

@Data
@Builder
@Slf4j
public class NotificationDTO {
  private Long id;
  private String title;
  private String message;
  private NotificationType type;
  private NotificationStatus status;

  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  private LocalDateTime timestamp;
  private String actionUrl;
  private Long targetUserId;

  private static final ObjectMapper objectMapper = new ObjectMapper();

  public static NotificationDTO from(Notification notification) {
    return NotificationDTO.builder()
                          .id(notification.getId())
                          .title(notification.getTitle())
                          .message(notification.getMessage())
                          .type(notification.getType())
                          .status(notification.getStatus())
                          .timestamp(notification.getTimestamp())
                          .actionUrl(notification.getActionUrl())
                          .targetUserId(notification.getTargetUserId())
                          .build();
  }

  public String toJson() {
    try {
      return objectMapper.writeValueAsString(this);
    } catch (Exception e) {
      log.error("Error converting notification to JSON", e);
      return String.format("{\"error\":\"Failed to serialize notification %d\"}", id);
    }
  }
}