/*
 *                                      Copyright 2025 WebPros International, LLC
 *                                                           All rights reserved.
 * copyright@cpanel.net                                         http://cpanel.net
 * This code is subject to the cPanel license. Unauthorized copying is prohibited.
 */

import { WHMAPI } from "@classes/WHMAPI";

/**
 * Plugin parameters for EcosystemFeatures API calls
 */
interface IPluginParams extends Record<string, string | number | boolean> {
    plugin: string;
    namespace?: string;
}

/**
 * This class facilitates use of WHMAPI1::EcosystemFeatures
 *
 * @example
 * ```typescript
 * const common = new CommonWhmapi()
 * ``
 */
export class CommonWhmapi extends WHMAPI {
    /**
     * [DUCKS-21]
     * Enable the specified plugin locally on the server
     * @param params - plugin parameters containing plugin name and optional namespace
     */
    async enableLocally(params: IPluginParams): Promise<void> {
        const response = await this.call(
            "EcosystemFeatures/local_enable",
            params
        );

        if (!response.isSuccess) {
            throw new Error(`Failed to enable ${params.plugin} \n ${response.error}`);
        }
    }

    /**
     * [DUCKS-21]
     * Disable the specified plugin locally on the server
     * @param params - plugin parameters containing plugin name and optional namespace
     */
    async disableLocally(params: IPluginParams): Promise<void> {
        const response = await this.call(
            "EcosystemFeatures/local_disable",
            params
        );

        if (!response.isSuccess) {
            throw new Error(`Failed to disable ${params.plugin} \n ${response.error}`);
        }
    }

    /**
     * [DUCKS-3160]
     * Reset the plugin [setting] to the cPanel & WHM default
     * @param params - plugin parameters containing plugin name and optional namespace
     */
    async resetLocally(params: IPluginParams): Promise<void> {
        const response = await this.call(
            "EcosystemFeatures/local_reset",
            params
        );

        if (!response.isSuccess) {
            throw new Error(`Failed to reset ${params.plugin} \n ${response.error}`);
        }
    }

    /**
     * Convenience method to enable Sitejet Beta features
     */
    async enableSitejetBeta(): Promise<void> {
        await this.enableLocally({ plugin: "cpanel-sitejet-plugin-beta", namespace: "Cpanel" });
    }

    /**
     * Convenience method to disable Sitejet Beta features
     */
    async disableSitejetBeta(): Promise<void> {
        await this.disableLocally({ plugin: "cpanel-sitejet-plugin-beta", namespace: "Cpanel" });
    }

    /**
     * Convenience method to reset Sitejet Beta features to WebPros rules
     */
    async resetSitejetBeta(): Promise<void> {
        await this.resetLocally({ plugin: "cpanel-sitejet-plugin-beta", namespace: "Cpanel" });
    }
}
