package com.atlassian.bitbucket.internal.webhook.json;

import com.atlassian.bitbucket.event.ApplicationEvent;
import com.atlassian.bitbucket.event.repository.RepositoryRefsChangedEvent;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.repository.RestRefChange;
import com.atlassian.bitbucket.rest.repository.RestRepository;
import com.atlassian.bitbucket.util.DateFormatter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

import static com.atlassian.bitbucket.webhook.WebHookEventId.REPOSITORY_REFS_CHANGED;

@JsonSurrogate(RepositoryRefsChangedEvent.class)
public class JsonRepositoryRefsChangedWebHook extends AbstractJsonWebHook {

    public static final String KEY_CHANGES = "changes";
    public static final String KEY_REPOSITORY = "repository";

    public JsonRepositoryRefsChangedWebHook(RepositoryRefsChangedEvent event, DateFormatter dateFormatter) {
        super((ApplicationEvent) event, REPOSITORY_REFS_CHANGED, dateFormatter);
        put(KEY_REPOSITORY, new RestRepository(event.getRepository(), true));
        put(KEY_CHANGES, ImmutableList.copyOf(Iterables.transform(event.getRefChanges(), RestRefChange::new)));
    }
}
