/*
 * 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.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.identity.api.resource.mgt.util.APIResourceManagementConfigBuilder;
import org.wso2.carbon.identity.application.common.model.APIResource;
import org.wso2.carbon.identity.application.common.model.Scope;
import org.wso2.carbon.identity.core.migrate.MigrationClientException;
import org.wso2.carbon.is.migration.service.v700.dao.APIResourceDAO;
import org.wso2.carbon.is.migration.service.v700.dao.ScopeDAO;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SystemAPIResourceCreationService {

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

    private static final APIResourceDAO apiResourceDAO = new APIResourceDAO();
    private static final ScopeDAO scopeDAO = new ScopeDAO();
    private final List<String> superTenantAPIs = Stream.of("Admin Advisory Management API",
                    "Tenant Management API").collect(Collectors.toList());

    public void execute() throws MigrationClientException {

        APIResourceManagementConfigBuilder apiResourceManagementConfigBuilder =
                APIResourceManagementConfigBuilder.getInstance();
        Map<String, APIResource> apiResourceMap = apiResourceManagementConfigBuilder.getAPIResourceMgtConfigurations();
        List<String> registeredSystemAPIs = apiResourceDAO.getSystemAPIResourceIdentifiers();
        // Register system API resources in super tenant.
        LOG.info("Started registering system API resources in the system");
        for (Map.Entry<String, APIResource> entry : apiResourceMap.entrySet()) {
            if (!registeredSystemAPIs.contains(entry.getValue().getIdentifier())) {
                registerAPIResource(entry.getValue());
            }
        }
        LOG.info("Completed registering system API resources in the system");
    }

    private void registerAPIResource(APIResource apiResource) {

        try {
            String apiResourceId = UUID.randomUUID().toString();
            Integer tenantId = null;
            if (superTenantAPIs.contains(apiResource.getName())) {
                tenantId = -1234;
            }
            apiResourceDAO.createAPIResource(apiResourceId, apiResource.getName(), apiResource.getIdentifier(),
                    tenantId, apiResource.getDescription(), apiResource.getType(),
                    apiResource.isAuthorizationRequired());
            List<Scope> scopes = apiResource.getScopes();
            scopeDAO.insertScopes(apiResourceId, scopes, tenantId);
        } catch (MigrationClientException e) {
            LOG.error("Error while registering API resource: " + apiResource.getName() + " in the system.", e);
        }
    }

}
