import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';
// Fetch settings from the API
export const fetchSettings = async () => {
	try {
		const response = await apiFetch( {
			path: '/suremails/v1/get-settings',
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
				'X-WP-Nonce': suremails.nonce,
			},
		} );

		if ( typeof response !== 'object' ) {
			throw new Error( 'Invalid JSON response' );
		}
		return response;
	} catch ( error ) {
		throw new Error( error.message || 'Error fetching settings' );
	}
};

// Save settings to the API
export const saveSettings = async ( updatedSettings ) => {
	try {
		const response = await apiFetch( {
			path: '/suremails/v1/set-settings',
			method: 'POST',
			headers: {
				'X-WP-Nonce': suremails.nonce,
				'Content-Type': 'application/json',
			},
			body: JSON.stringify( {
				settings: {
					delete_email_logs_after:
						updatedSettings.settings.delete_email_logs_after,
					log_emails: updatedSettings.settings.log_emails,
					email_simulation: updatedSettings.settings.email_simulation,
					default_connection:
						updatedSettings.settings.default_connection,
					emailSummaryActive:
						updatedSettings.settings.emailSummaryActive,
					emailSummaryDay: updatedSettings.settings.emailSummaryDay,
					showInSidebar: updatedSettings.settings.showInSidebar,
					analytics: updatedSettings.settings.analytics,
				},
			} ),
		} );
		return response;
	} catch ( error ) {
		throw new Error( error.message || 'Error saving settings' );
	}
};

/**
 * Set the Reputation Shield (Content Guard) activation status.
 *
 * When a boolean `status` is passed, the backend is instructed to set the
 * option to that exact value. If omitted, the endpoint preserves its legacy
 * toggle behavior for backward compatibility.
 *
 * @param {boolean} [status] Desired activation state.
 * @return {Object} The response
 */
export const activateContentGuard = async ( status ) => {
	try {
		const hasStatus = typeof status === 'boolean';
		return await apiFetch( {
			path: '/suremails/v1/content-guard/activate',
			method: hasStatus ? 'POST' : 'GET',
			headers: {
				'Content-Type': 'application/json',
				'X-WP-Nonce': suremails.nonce,
			},
			...( hasStatus && {
				data: { status: status ? 'yes' : 'no' },
			} ),
		} );
	} catch ( error ) {
		throw new Error(
			error?.data?.message ||
				__( 'Error activating Reputation Shield', 'suremails' )
		);
	}
};

/**
 * Save user details for Content Guard
 *
 * @param {Object} userDetails - The user details
 * @return {Object} The response
 */
export const saveUserDetails = async ( userDetails ) => {
	try {
		return await apiFetch( {
			path: '/suremails/v1/content-guard/user-details',
			method: 'POST',
			headers: {
				'X-WP-Nonce': suremails.nonce,
				'Content-Type': 'application/json',
			},
			body: JSON.stringify( userDetails ),
		} );
	} catch ( error ) {
		throw new Error( error.data.message || 'Error saving user details' );
	}
};
