package io.delphiplatform.api.v3.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import org.openapitools.jackson.nullable.JsonNullable;

import java.time.LocalDateTime;

import io.delphiplatform.api.v3.constant.ApplicationConstants;
import io.delphiplatform.api.v3.rdb.entity.BasePlaylistLifetimePublicEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
@JsonPropertyOrder(alphabetic = true)
public class PlaylistDates {

    @JsonProperty("dsp_update_date_time")
    @JsonFormat(shape = Shape.STRING, pattern = ApplicationConstants.ISO_DATE_TIME_PATTERN )
    private JsonNullable<LocalDateTime> dspUpdateDateTime = JsonNullable.undefined();

    @JsonProperty("playlist_updated_at_date_time")
    @JsonFormat(shape = Shape.STRING, pattern = ApplicationConstants.ISO_DATE_TIME_PATTERN )
    private JsonNullable<LocalDateTime> playlistUpdatedAtDateTime = JsonNullable.undefined();

    @JsonProperty("dsp_sync_date_time")
    @JsonFormat(shape = Shape.STRING, pattern = ApplicationConstants.ISO_DATE_TIME_PATTERN )
    private JsonNullable<LocalDateTime> dspSyncDateTime = JsonNullable.undefined();

    public static PlaylistDates from(BasePlaylistLifetimePublicEntity entity, LocalDateTime dspSyncDateTime) {
        if (entity == null) {
            return null;
        }

        return new PlaylistDates(
            entity.getDspUpdateDateTime() != null ? JsonNullable.of(entity.getDspUpdateDateTime()) : null,
            entity.getPlaylistUpdatedAtDateTime() != null ? JsonNullable.of(entity.getPlaylistUpdatedAtDateTime()) : null,
            (dspSyncDateTime != null) ? JsonNullable.of(dspSyncDateTime) : JsonNullable.undefined());
    }

    public static PlaylistDates from(LocalDateTime dspSyncDateTime) {
        if (dspSyncDateTime == null) {
            return null;
        }

        return new PlaylistDates(JsonNullable.undefined(), JsonNullable.undefined(),
            (dspSyncDateTime != null) ? JsonNullable.of(dspSyncDateTime) : JsonNullable.undefined());
    }
}
