package com.atlassian.bitbucket.internal.branch.model.dao;

import com.atlassian.bitbucket.internal.branch.model.BranchTypes;
import com.google.common.collect.ImmutableMap;
import net.java.ao.EntityManager;
import net.java.ao.test.jdbc.DatabaseUpdater;

import java.sql.SQLException;

public class BranchModelTestData implements DatabaseUpdater {

    static final int FULL_BRANCH_MODEL_REPOSITORY_ID = 1;
    static final int EMPTY_MODEL_REPOSITORY_ID = 2;

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

        // one with production branch and all types enabled
        entityManager.create(AoBranchModelConfiguration.class, ImmutableMap.<String, Object>builder()
                .put(AoBranchModelConfiguration.REPOSITORY_ID_COLUMN, FULL_BRANCH_MODEL_REPOSITORY_ID)
                .put(AoBranchModelConfiguration.ENABLED_COLUMN, true)
                .put(AoBranchModelConfiguration.DEVELOPMENT_BRANCH_REF_ID_COLUMN, "refs/heads/develop")
                .put(AoBranchModelConfiguration.DEVELOPMENT_BRANCH_USE_DEFAULT_COLUMN, false)
                .put(AoBranchModelConfiguration.PRODUCTION_BRANCH_REF_ID_COLUMN, "refs/heads/master")
                .put(AoBranchModelConfiguration.PRODUCTION_BRANCH_USE_DEFAULT_COLUMN, false)
                .build()
        );

        createType(entityManager, FULL_BRANCH_MODEL_REPOSITORY_ID, BranchTypes.BUGFIX, "bugfix/", true);
        createType(entityManager, FULL_BRANCH_MODEL_REPOSITORY_ID, BranchTypes.FEATURE, "feature/", true);
        createType(entityManager, FULL_BRANCH_MODEL_REPOSITORY_ID, BranchTypes.HOTFIX, "hotfix/", true);
        createType(entityManager, FULL_BRANCH_MODEL_REPOSITORY_ID, BranchTypes.RELEASE, "release/", true);

        // one with no production branch, default dev and no enabled types
        entityManager.create(AoBranchModelConfiguration.class, ImmutableMap.<String, Object>builder()
                .put(AoBranchModelConfiguration.REPOSITORY_ID_COLUMN, EMPTY_MODEL_REPOSITORY_ID)
                .put(AoBranchModelConfiguration.ENABLED_COLUMN, true)
                .put(AoBranchModelConfiguration.DEVELOPMENT_BRANCH_USE_DEFAULT_COLUMN, true)
                .put(AoBranchModelConfiguration.PRODUCTION_BRANCH_USE_DEFAULT_COLUMN, false)
                .build()
        );

        createType(entityManager, EMPTY_MODEL_REPOSITORY_ID, BranchTypes.BUGFIX, "emptybugfix/", false);
        createType(entityManager, EMPTY_MODEL_REPOSITORY_ID, BranchTypes.FEATURE, "emptybugfix/", false);
        createType(entityManager, EMPTY_MODEL_REPOSITORY_ID, BranchTypes.HOTFIX, "emptybugfix/", false);
        createType(entityManager, EMPTY_MODEL_REPOSITORY_ID, BranchTypes.RELEASE, "emptybugfix/", false);
    }

    private void createType(EntityManager entityManager, long branchModelId, BranchTypes type, String prefix,
                                boolean enabled) throws SQLException {
        entityManager.create(AoBranchTypeConfiguration.class, ImmutableMap.<String, Object>builder()
                .put(AoBranchTypeConfiguration.BRANCH_MODEL_ID_COLUMN, branchModelId)
                .put(AoBranchTypeConfiguration.TYPE_ID_COLUMN, type.getId())
                .put(AoBranchTypeConfiguration.TYPE_PREFIX_COLUMN, prefix)
                .put(AoBranchTypeConfiguration.TYPE_ENABLED_COLUMN, enabled)
                .build()
        );
    }
}
