/*
 * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). All Rights Reserved.
 *
 * This software is the property of WSO2 LLC. and its suppliers, if any.
 * Dissemination of any information or reproduction of any material contained
 * herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
 * You may not alter or remove any copyright or other notice from copies of this content.
 */

package org.wso2.carbon.is.migration.service.v700.migrator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.identity.core.migrate.MigrationClientException;
import org.wso2.carbon.is.migration.service.Migrator;
import org.wso2.carbon.is.migration.service.v700.dao.GroupDAO;
import org.wso2.carbon.is.migration.service.v700.model.Group;
import org.wso2.carbon.is.migration.util.Constant;
import org.wso2.carbon.is.migration.util.ReportUtil;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

import static org.wso2.carbon.is.migration.util.Constant.MIGRATION_LOG;
import static org.wso2.carbon.is.migration.util.Constant.REPORT_PATH;

public class GroupUUIDMigrator extends Migrator {

    private static final Logger LOG = LoggerFactory.getLogger(GroupUUIDMigrator.class);
    private final GroupDAO groupDAO = new GroupDAO();

    @Override
    public void dryRun() throws MigrationClientException {

        try {
            LOG.info(Constant.MIGRATION_LOG + "Executing dry run for {}", this.getClass().getName());
            Properties migrationProperties = getMigratorConfig().getParameters();
            String reportPath = (String) migrationProperties.get(REPORT_PATH);
            ReportUtil reportUtil = new ReportUtil(reportPath);
            reportUtil.writeMessage("\n--- Summary of the report ---\n");
            LOG.info(Constant.MIGRATION_LOG + "Started the dry run for {} migration.",
                    this.getClass().getName());

            reportUtil.writeMessage("Checking the role names other than internal and primary.");
            LOG.info(Constant.MIGRATION_LOG + "Checking the roles other than internal and primary.");

            List<String> otherRoleNames = groupDAO.getOtherRoleNames();

            if (otherRoleNames.isEmpty()) {
                reportUtil.writeMessage("No issues with the current role names.");
            } else {
                reportUtil.writeMessage("The following roles will not be migrated.");
                for (String otherRoleName : otherRoleNames) {
                    reportUtil.writeMessage(otherRoleName);
                }
            }
            reportUtil.commit();
        } catch (IOException e) {
            throw new MigrationClientException("Error while writing the dry run report.", e);
        }
    }

    @Override
    public void migrate() throws MigrationClientException {

        try {
            LOG.info(MIGRATION_LOG + "Starting the migration of group attributes.");
            List<Group> groupList = groupDAO.getGroups();
            for (Group group : groupList) {
                groupDAO.resolveAttributesFromScimData(group);
                groupDAO.updateGroupAttributesInUserStore(group);
            }
            LOG.info(MIGRATION_LOG + "Completed the Updating the group attributes.");
        } catch (Error e) {
            throw new MigrationClientException("Error occurred when updating the group attributes.", e);
        }
    }
}
