package com.atlassian.stash.internal.comment.like.dao;

import com.google.common.collect.ImmutableMap;
import net.java.ao.EntityManager;
import net.java.ao.test.jdbc.DatabaseUpdater;

import java.sql.SQLException;

public class CommentLikeTestData implements DatabaseUpdater {

    public static final long LIKED_COMMENT_1_ID = 1L;
    public static final long LIKED_COMMENT_2_ID = 2L;

    public static final int COMMENT_1_LIKER_1_ID = 11;
    public static final int COMMENT_1_LIKER_2_ID = 12;
    public static final int COMMENT_2_LIKER_1_ID = 12; // same user liked both comments
    public static final int COMMENT_2_LIKER_2_ID = 21;
    public static final int COMMENT_2_LIKER_3_ID = 22;

    @SuppressWarnings("unchecked")
    @Override
    public void update(EntityManager entityManager) throws Exception {
        entityManager.migrate(AoCommentLike.class);

        createLike(entityManager, LIKED_COMMENT_1_ID, COMMENT_1_LIKER_1_ID);
        createLike(entityManager, LIKED_COMMENT_1_ID, COMMENT_1_LIKER_2_ID);

        createLike(entityManager, LIKED_COMMENT_2_ID, COMMENT_2_LIKER_1_ID);
        createLike(entityManager, LIKED_COMMENT_2_ID, COMMENT_2_LIKER_2_ID);
        createLike(entityManager, LIKED_COMMENT_2_ID, COMMENT_2_LIKER_3_ID);
    }

    private void createLike(EntityManager entityManager, long commentId, int userId) throws SQLException {
        entityManager.create(AoCommentLike.class, ImmutableMap.<String, Object>of(
                AoCommentLike.COMMENT_ID_COLUMN, commentId,
                AoCommentLike.USER_ID_COLUMN, userId
        ));
    }
}
