<?php
/**
 * Admin subsystem bootstrap.
 *
 * @package Donatico\Admin
 */

declare(strict_types=1);

namespace Donatico\Admin;

use Donatico\Database\DonationRepository;
use Donatico\Interfaces\HookableInterface;
use Donatico\Loader;

/**
 * Instantiates all admin-side components and wires their hooks
 * into the shared Loader instance.
 */
class Admin implements HookableInterface {

	/** @var Loader */
	private Loader $loader;

	/**
	 * @param Loader $loader Shared hook loader.
	 */
	public function __construct( Loader $loader ) {
		$this->loader = $loader;
	}

	/**
	 * Build all admin objects and register their hooks.
	 *
	 * @return void
	 */
	public function registerHooks(): void {
		$repository = new DonationRepository();
		$assets     = new Assets();
		$dashboard  = new Dashboard( $repository );
		$settings   = new Settings();
		$designer   = new FormDesigner();

		// DonationsList is NOT instantiated here — it extends WP_List_Table and
		// requires admin screens to be ready. Menu creates it lazily inside the
		// page callback closure (see Menu::registerMenus).
		$upgrade_page   = new UpgradePage();
		$teasers        = new Teasers();
		$menu           = new Menu( $dashboard, $repository, $settings, $designer, $upgrade_page, $teasers );
		$upsell_prompts = new UpsellPrompts();

		// Menu.
		$this->loader->addAction( 'admin_menu', $menu, 'registerMenus' );
		$this->loader->addAction( 'admin_menu', $menu, 'registerCampaignsTeaser', 20 );
		$this->loader->addAction( 'admin_menu', $menu, 'registerTiersTeaser', 21 );
		$this->loader->addAction( 'admin_menu', $menu, 'registerDonorsTeaser', 22 );
		$this->loader->addAction( 'admin_menu', $menu, 'registerRecurringTeaser', 23 );
		$this->loader->addAction( 'admin_menu', $menu, 'registerSettingsMenu', 99 );
		$this->loader->addAction( 'admin_menu', $menu, 'registerUpgradeMenu', 100 );

		// Assets (only loads on plugin pages — Assets::enqueue checks the hook suffix).
		$this->loader->addAction( 'admin_enqueue_scripts', $assets, 'enqueue' );

		// Settings API.
		$this->loader->addAction( 'admin_init', $settings, 'registerSettings' );

		// Upsell prompts — contextual notices and dismiss handler.
		$this->loader->addAction( 'admin_notices', $upsell_prompts, 'renderDonationsNotice' );
		$this->loader->addAction( 'wp_ajax_donatico_dismiss_upsell', $upsell_prompts, 'ajaxDismiss' );

		// AJAX — Form Designer live preview.
		$this->loader->addAction( 'wp_ajax_donatico_designer_preview', $designer, 'ajaxPreview' );

		// AJAX — Stripe connection test.
		$this->loader->addAction( 'wp_ajax_donatico_test_stripe_connection', $settings, 'ajaxTestConnection' );

		// AJAX — Send test emails (admin + donor previews).
		$this->loader->addAction( 'wp_ajax_donatico_send_test_email', $settings, 'ajaxSendTestEmail' );

		// Plugin action links (Settings shortcut on the Plugins page).
		$this->loader->addFilter(
			'plugin_action_links_' . DONATICO_PLUGIN_BASENAME,
			$this,
			'addPluginActionLinks'
		);
	}

	/**
	 * Prepend a Settings link to the plugin's action links on Plugins → Installed Plugins.
	 *
	 * @param array<int, string> $links
	 * @return array<int, string>
	 */
	public function addPluginActionLinks( array $links ): array {
		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			\esc_url( \admin_url( 'admin.php?page=donatico-settings' ) ),
			\esc_html__( 'Settings', 'donatico' )
		);

		\array_unshift( $links, $settings_link );

		if ( ! donatico_can_use_premium() ) {
			$upgrade_link = sprintf(
				'<a href="%s" style="color: #2DD4A8; font-weight: 600;">%s</a>',
				\esc_url( \admin_url( 'admin.php?page=donatico-upgrade' ) ),
				\esc_html__( 'Get Pro', 'donatico' )
			);
			\array_unshift( $links, $upgrade_link );
		}

		return $links;
	}
}
