<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Sifency_Admin_Button_Generator {

	/**
	 * Creates a button to trigger an import/restore action.
	 *
	 * @param string $type The action type (e.g., 'wxr_import', 'admin_options').
	 * @param string $label The button text.
	 * @param string $section_id The CSF section slug to redirect back to.
	 * @return string The button HTML.
	 */
	public static function create_import_button( $type, $label, $section_id = '' ) {
		if ( 'elementor_kit' === $type && ! did_action( 'elementor/loaded' ) ) {
			return '<button class="button" disabled>Import Elementor Kit</button><p><small><em>' . esc_html__( 'Elementor plugin is not active.', 'sifency-addons' ) . '</em></small></p>';
		}

		$query_args = array( 'import-suite-action' => $type );
		if ( ! empty( $section_id ) ) {
			$query_args['current_tab'] = $section_id;
		}
		$url_with_action = add_query_arg( $query_args );
		$secure_url      = wp_nonce_url( $url_with_action, "import_{$type}_nonce" );

		return sprintf(
			'<a href="%s" class="button button-primary sifency-admin-btn sifency-addons-export-import-btn"><span class="dashicons dashicons-database-import"></span> %s</a>',
			esc_url( $secure_url ),
			esc_html( $label )
		);
	}

	/**
	 * Creates a button to trigger an export/backup action.
	 *
	 * @return string The button HTML.
	 */
	public static function create_export_button( $type, $label, $section_id = '' ) {
		if ( 'elementor_kit' === $type && ! did_action( 'elementor/loaded' ) ) {
			return '<button class="button sifency-admin-btn sifency-addons-export-import-btn" disabled>Update Elementor Kit</button><p><small><em>' . esc_html__( 'Elementor plugin is not active.', 'sifency-addons' ) . '</em></small></p>';
		}

		$query_args = array( 'export-suite-action' => $type );
		if ( ! empty( $section_id ) ) {
			$query_args['current_tab'] = $section_id;
		}
		$url_with_action = add_query_arg( $query_args );

		$secure_url = wp_nonce_url( $url_with_action, "export_{$type}_nonce" );

		return sprintf(
			'<a href="%s" class="button button-primary sifency-admin-btn sifency-addons-export-import-btn">%s</a>',
			esc_url( $secure_url ),
			esc_html( $label )
		);
	}

	/**
	 * Creates a link to directly download an existing demo file from the uploads directory.
	 *
	 * @param string $type The unique action identifier (e.g., 'wxr', 'meta-options').
	 * @param string $label The text for the button.
	 * @return string The HTML for the download button link.
	 */
	public static function create_download_button( $type, $label ) {
		if ( ! class_exists( 'Sifency_Demo_Helper' ) ) {
			return '<a href="#" class="button button-secondary sifency-admin-btn sifency-secondary-btn" disabled>Error: Helper class is missing.</a>';
		}

		$file_path = Sifency_Demo_Helper::get_filepath( $type );

		if ( empty( $file_path ) || ! file_exists( $file_path ) ) {

			$debug_message = 'File not found at: ' . esc_attr( $file_path );

			return '<a href="#" class="button button-secondary sifency-admin-btn sifency-secondary-btn" disabled title="' . $debug_message . '">'
				. esc_html( $label )
				. '</a>';
		}

		$url_with_action = add_query_arg( 'download-suite-action', $type );
		$secure_url      = wp_nonce_url( $url_with_action, "download_{$type}_nonce" );

		return sprintf(
			'<a href="%s" class="button button-secondary sifency-admin-btn sifency-secondary-btn"><span class="dashicons dashicons-download"></span> %s</a>',
			esc_url( $secure_url ),
			esc_html( $label )
		);
	}

	/**
	 * Creates the RESET button link with a unique ID for JavaScript targeting.
	 *
	 * @return string The button HTML.
	 */
	public static function create_reset_button( $type, $label, $section_id = '' ) {
		$query_args = array( 'reset-suite-action' => $type );
		if ( ! empty( $section_id ) ) {
			$query_args['current_tab'] = $section_id;
		}
		$url_with_action = add_query_arg( $query_args );
		$secure_url      = wp_nonce_url( $url_with_action, "reset_{$type}_nonce" );
		$button_id       = 'sifency-reset-settings-button';

		return sprintf(
			'<a href="%s" id="%s" class="button button-danger sifency-admin-btn"><span class="dashicons dashicons-trash"></span> %s</a>',
			esc_url( $secure_url ),
			esc_attr( $button_id ),
			esc_html( $label )
		);
	}
}


/**
 * Gets the standardized filename for a given export type.
 * This is the missing helper function required by the Button Generator class.
 *
 * @param string $type The type of export (e.g., 'wxr', 'customizer').
 * @return string The corresponding filename.
 */
function sifency_addons_get_export_filename( $type ) {
	$theme = wp_get_theme();
	if ( $theme->parent() ) {
		$theme = $theme->parent();
	}
	$theme_slug = $theme->get( 'TextDomain' );

	$filename = '';

	switch ( $type ) {
		case 'wxr':
			$filename = $theme_slug . '-content.xml';
			break;
		case 'customizer':
			$filename = 'customizer.dat';
			break;
		case 'widgets':
			$filename = 'widgets.wie';
			break;
		case 'elementor_kit':
			$filename = 'kit-settings.json';
			break;
		case 'meta-options':
			$filename = $theme_slug . '-meta.json';
			break;
		case 'admin_options':
			$filename = $theme_slug . '-options.json';
			break;
		default:
			$filename = '';
			break;
	}

	return $filename;
}
