package com.atlassian.bitbucket.internal.audit.rest;

import com.atlassian.bitbucket.audit.MinimalAuditEntry;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import com.atlassian.bitbucket.rest.util.RestPage;
import com.atlassian.bitbucket.rest.user.RestPerson;
import com.atlassian.bitbucket.user.ApplicationUser;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.Date;

import static com.atlassian.bitbucket.rest.util.RestDocUtils.pageOf;

@JsonSerialize
@JsonSurrogate(MinimalAuditEntry.class)
public class RestMinimalAuditEntry extends RestMapEntity {

    private static final String USER = "user";
    private static final String ACTION = "action";
    private static final String TIMESTAMP = "timestamp";
    private static final String DETAILS = "details";

    @SuppressWarnings("unused")
    public RestMinimalAuditEntry() {
    }

    public RestMinimalAuditEntry(MinimalAuditEntry event) {
        this(event.getUser(), event.getAction(), event.getTimestamp(), event.getDetails());
    }

    private RestMinimalAuditEntry(ApplicationUser user, String action, Date timestamp, String details) {
        put(ACTION, action);
        put(TIMESTAMP, timestamp);
        putIfNotNull(DETAILS, details);
        putIfNotNull(USER, RestPerson.REST_TRANSFORM.apply(user));
    }

    public static final RestMinimalAuditEntry RESPONSE_EXAMPLE = new RestMinimalAuditEntry(
            RestApplicationUser.RESPONSE_EXAMPLE, "RepositoryPush", new Date(), "Details");

    public static final RestPage<RestMinimalAuditEntry> PAGE_EXAMPLE = pageOf(RESPONSE_EXAMPLE);
}
