/*
 * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
 *
 * WSO2 LLC. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.identity.core.migrate.MigrationClientException;
import org.wso2.carbon.identity.system.config.mgt.remotelogging.DBBasedRemoteLoggingConfigDAO;
import org.wso2.carbon.is.migration.service.Migrator;
import org.wso2.carbon.logging.service.LoggingConstants;
import org.wso2.carbon.logging.service.RemoteLoggingServerException;
import org.wso2.carbon.logging.service.dao.RemoteLoggingConfigDAO;
import org.wso2.carbon.logging.service.dao.impl.RegistryBasedRemoteLoggingConfigDAO;
import org.wso2.carbon.logging.service.data.RemoteServerLoggerData;

import java.util.Optional;

/**
 * This class is to migrate the remote logging configuration.
 */
public class RemoteLoggingConfigMigrator extends Migrator {

    private static final Logger LOG = LoggerFactory.getLogger(RemoteLoggingConfigMigrator.class);

    private RemoteLoggingConfigDAO registryBasedLoggingConfigDAO = new RegistryBasedRemoteLoggingConfigDAO();
    private RemoteLoggingConfigDAO dbBasedLoggingConfigDAO = new DBBasedRemoteLoggingConfigDAO();

    @Override
    public void dryRun() throws MigrationClientException {

        LOG.info("RDM-RLC-DR>> Residual data migration (dry run) of remote logging configs started.");
        migrateRemoteLoggingConfigs(true);
    }

    @Override
    public void migrate() throws MigrationClientException {

        LOG.info("RDM-RLC>> Residual data migration of remote logging configs started.");
        migrateRemoteLoggingConfigs(false);
    }

    private void migrateRemoteLoggingConfigs(boolean isDryRun) throws MigrationClientException {

        migrateLogConfigForLogType(LoggingConstants.CARBON, isDryRun);
        migrateLogConfigForLogType(LoggingConstants.AUDIT, isDryRun);
    }

    private void migrateLogConfigForLogType(String type, boolean isDryRun) throws MigrationClientException {

        LoggingConstants.LogType logType = LoggingConstants.LogType.valueOf(type);
        try {
            if (dbBasedLoggingConfigDAO.getRemoteServerConfig(logType).isPresent()) {
                LOG.warn("RDM-RLC>> Log configs already exists in database for log type: {}. Skipping migration from " +
                        "registry.", logType);
                return;
            }

            Optional<RemoteServerLoggerData> logConfig = registryBasedLoggingConfigDAO.getRemoteServerConfig(logType);
            if (logConfig.isPresent()) {
                if (!isDryRun) {
                    dbBasedLoggingConfigDAO.saveRemoteServerConfig(logConfig.get(), logType);
                    LOG.info("RDM-RLC>> Remote server configuration migration for log type: {} completed successfully.",
                            logType);
                } else {
                    LOG.info("RDM-RLC>> Remote server configuration migration for log type: {} skipped due to dry run.",
                            logType);
                }
            } else {
                LOG.info("RDM-RLC>> No remote server configuration for log type: {} found in registry.", logType);
            }
        } catch (RemoteLoggingServerException e) {
            throw new MigrationClientException(
                    String.format("Error while migrating %s log configuration to the new DAO", logType), e);
        }
    }
}
