<?php
/**
 * Dobare Plugin Action Links
 *
 * Handles custom action links in the WordPress plugins list page.
 *
 * @package Dobare
 */

namespace Dobare\Admin;

defined('ABSPATH') || exit;

/**
 * Class Dobare_Plugin_Action_Links
 */
class Dobare_Plugin_Action_Links {

	/**
	 * Constructor
	 */
	public function __construct() {
		add_filter('plugin_action_links_' . plugin_basename(DOBARE_MAIN_PLUGIN_FILE), array($this, 'add_action_links'));
		add_action('admin_enqueue_scripts', array($this, 'enqueue_modal_assets'));
		add_action('admin_footer', array($this, 'render_modal_container'));
	}

	/**
	 * Add custom action links to plugin row
	 *
	 * @param array $links Existing plugin action links.
	 * @return array Modified plugin action links.
	 */
	public function add_action_links($links) {
		$custom_links = array(
			'credit_disable' => '<a href="#" id="dobare-credit-disable-modal-trigger" class="dobare-credit-disable-link">' . esc_html__('غیرفعال کردن اعمال اعتبار', 'dobare') . '</a>',
		);

		return array_merge($custom_links, $links);
	}

	/**
	 * Enqueue modal assets only on plugins page
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_modal_assets($hook) {
		// Only load on plugins.php page
		if ('plugins.php' !== $hook) {
			return;
		}

		// Enqueue the main plugin build (which includes our modal)
		$asset_file = include plugin_dir_path(DOBARE_MAIN_PLUGIN_FILE) . 'build/index.asset.php';

		wp_enqueue_script(
			'dobare-plugin-modal',
			plugins_url('build/index.js', DOBARE_MAIN_PLUGIN_FILE),
			$asset_file['dependencies'],
			$asset_file['version'],
			true
		);

		wp_enqueue_style(
			'dobare-plugin-modal-style',
			plugins_url('build/index.css', DOBARE_MAIN_PLUGIN_FILE),
			array(),
			$asset_file['version']
		);

		// Pass necessary data to JavaScript
		wp_localize_script(
			'dobare-plugin-modal',
			'dobarePluginModal',
			array(
				'nonce' => wp_create_nonce('wp_rest'),
				'apiUrl' => rest_url(),
				'isPluginsPage' => true,
			)
		);
	}

	/**
	 * Render modal container in admin footer
	 */
	public function render_modal_container() {
		$screen = get_current_screen();
		
		// Only render on plugins page
		if (!$screen || 'plugins' !== $screen->id) {
			return;
		}

		echo '<div id="dobare-credit-disable-modal-root"></div>';
	}
}

