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

import org.wso2.carbon.identity.core.migrate.MigrationClientException;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.is.migration.service.v610.model.OAuthApp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * OAuthApp DAO class.
 */
public class OAuthAppDAO {

    private static final String ID = "ID";
    private static final String GRANT_TYPES = "GRANT_TYPES";
    private static final String GET_OAUTH_CONSUMER_APP_GRANTS = "SELECT ID, GRANT_TYPES FROM IDN_OAUTH_CONSUMER_APPS " +
            "WHERE TENANT_ID = ?";
    private static final String UPDATE_OAUTH_CONSUMER_APP_GRANTS = "UPDATE IDN_OAUTH_CONSUMER_APPS SET " +
            "GRANT_TYPES = ? WHERE ID = ?";

    /**
     * Get OAuth consumer apps with grant types.
     *
     * @param tenantID Tenant id.
     * @return List of OAuthApps.
     * @throws MigrationClientException If an error occurs while retrieving the allowed grant types of the app.
     */
    public List<OAuthApp> getOAuthConsumerAppsWithGrants(int tenantID) throws MigrationClientException {

        try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
            try (PreparedStatement preparedStatement = connection.prepareStatement(GET_OAUTH_CONSUMER_APP_GRANTS)) {
                preparedStatement.setInt(1, tenantID);
                ResultSet resultSet = preparedStatement.executeQuery();
                List<OAuthApp> oauthApps = new ArrayList<>();
                while (resultSet.next()) {
                    OAuthApp oauthApp = new OAuthApp();
                    oauthApp.setOauthAppId(resultSet.getInt(ID));
                    oauthApp.setTenantId(tenantID);
                    oauthApp.setGrantTypes(resultSet.getString(GRANT_TYPES));
                    oauthApps.add(oauthApp);
                }
                return oauthApps;
            }
        } catch (SQLException e) {
            throw new MigrationClientException("Error while retrieving the allowed grant types of the app.", e);
        }
    }

    /**
     * Update the allowed grant types of the app.
     *
     * @param grantTypes Grant types.
     * @param oauthAppId OAuth app id.
     * @throws MigrationClientException If an error occurs while updating the allowed grant types of the app.
     */
    public void updateOAuthConsumerAppGrants(String grantTypes, int oauthAppId)
            throws MigrationClientException {

        try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
            try (PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_OAUTH_CONSUMER_APP_GRANTS)) {
                preparedStatement.setString(1, grantTypes);
                preparedStatement.setInt(2, oauthAppId);
                preparedStatement.execute();
            }
        } catch (SQLException e) {
            throw new MigrationClientException("Error while updating the allowed grant types of the app.", e);
        }
    }
}
