<?php
/**
 * Admin menu registration.
 *
 * @package Donatico\Admin
 */

declare(strict_types=1);

namespace Donatico\Admin;

use Donatico\Database\DonationRepository;

/**
 * Registers the Donatico top-level admin menu and all submenus.
 *
 * DonationsList is NOT stored as a property — it extends WP_List_Table and
 * must only be instantiated inside a page-render callback (after admin screens
 * are initialised), never during plugins_loaded or admin_menu hook registration.
 */
class Menu {

	/** @var Dashboard */
	private Dashboard $dashboard;

	/** @var DonationRepository Passed to DonationsList at render time. */
	private DonationRepository $repository;

	/** @var Settings */
	private Settings $settings;

	/** @var FormDesigner */
	private FormDesigner $designer;

	/** @var UpgradePage */
	private UpgradePage $upgradePage;

	/** @var Teasers */
	private Teasers $teasers;

	/**
	 * @param Dashboard          $dashboard
	 * @param DonationRepository $repository  Injected so DonationsList can be built lazily.
	 * @param Settings           $settings
	 * @param FormDesigner       $designer
	 * @param UpgradePage        $upgradePage
	 * @param Teasers            $teasers
	 */
	public function __construct(
		Dashboard $dashboard,
		DonationRepository $repository,
		Settings $settings,
		FormDesigner $designer,
		UpgradePage $upgradePage,
		Teasers $teasers
	) {
		$this->dashboard   = $dashboard;
		$this->repository  = $repository;
		$this->settings    = $settings;
		$this->designer    = $designer;
		$this->upgradePage = $upgradePage;
		$this->teasers     = $teasers;
	}

	/**
	 * Register all admin menus.
	 * Hooked to admin_menu.
	 *
	 * @return void
	 */
	public function registerMenus(): void {
		// Top-level menu — also acts as the Dashboard page.
		add_menu_page(
			__( 'Donatico', 'donatico' ),
			__( 'Donatico', 'donatico' ),
			'manage_options',
			'donatico',
			array( $this->dashboard, 'render' ),
			$this->getMenuIcon(),
			30
		);

		// Rename the auto-generated first submenu item.
		add_submenu_page(
			'donatico',
			__( 'Dashboard — Donatico', 'donatico' ),
			__( 'Dashboard', 'donatico' ),
			'manage_options',
			'donatico',
			array( $this->dashboard, 'render' )
		);

		add_submenu_page(
			'donatico',
			__( 'Donations — Donatico', 'donatico' ),
			__( 'Donations', 'donatico' ),
			'manage_options',
			'donatico-donations',
			function () {
				// DonationsList extends WP_List_Table — instantiate here, inside the
				// page callback, so convert_to_screen() is available (screens are
				// initialised by the time WordPress calls this closure).
				( new DonationsList( $this->repository ) )->render();
			}
		);

		add_submenu_page(
			'donatico',
			__( 'Form Designer — Donatico', 'donatico' ),
			__( 'Form Designer', 'donatico' ),
			'manage_options',
			'donatico-form-designer',
			array( $this->designer, 'render' )
		);

	}

	/**
	 * Register the Campaigns teaser submenu when Pro is inactive.
	 * Hooked to admin_menu at priority 20.
	 *
	 * @return void
	 */
	public function registerCampaignsTeaser(): void {
		if ( ! donatico_can_use_premium() ) {
			add_submenu_page(
				'donatico',
				__( 'Campaigns', 'donatico' ),
				__( 'Campaigns', 'donatico' ),
				'manage_options',
				'donatico-campaigns-teaser',
				array( $this->teasers, 'renderCampaigns' )
			);
		}
	}

	/**
	 * Register the Tiers teaser submenu when Pro is inactive.
	 * Hooked to admin_menu at priority 21.
	 *
	 * @return void
	 */
	public function registerTiersTeaser(): void {
		if ( ! donatico_can_use_premium() ) {
			add_submenu_page(
				'donatico',
				__( 'Donation Tiers', 'donatico' ),
				__( 'Tiers', 'donatico' ),
				'manage_options',
				'donatico-tiers-teaser',
				array( $this->teasers, 'renderTiers' )
			);
		}
	}

	/**
	 * Register the Recurring Donations teaser submenu when Pro is inactive.
	 * Hooked to admin_menu at priority 23.
	 *
	 * Priority 23 is also used by the premium SubscriptionsAdminPage in PremiumLoader,
	 * but that only registers when Pro is ACTIVE; this teaser only registers when Pro
	 * is INACTIVE — the two are mutually exclusive via donatico_can_use_premium().
	 *
	 * @return void
	 */
	public function registerRecurringTeaser(): void {
		if ( ! donatico_can_use_premium() ) {
			add_submenu_page(
				'donatico',
				__( 'Recurring', 'donatico' ),
				__( 'Recurring', 'donatico' ),
				'manage_options',
				'donatico-recurring-teaser',
				array( $this->teasers, 'renderRecurring' )
			);
		}
	}

	/**
	 * Register the Donors teaser submenu when Pro is inactive.
	 * Hooked to admin_menu at priority 22.
	 *
	 * @return void
	 */
	public function registerDonorsTeaser(): void {
		if ( ! donatico_can_use_premium() ) {
			add_submenu_page(
				'donatico',
				__( 'Donors', 'donatico' ),
				__( 'Donors', 'donatico' ),
				'manage_options',
				'donatico-donors-teaser',
				array( $this->teasers, 'renderDonors' )
			);
		}
	}

	/**
	 * Register the Upgrade to Pro submenu when Pro is inactive.
	 * Hooked to admin_menu at priority 100 — after Settings (99).
	 *
	 * @return void
	 */
	public function registerUpgradeMenu(): void {
		if ( ! donatico_can_use_premium() ) {
			add_submenu_page(
				'donatico',
				__( 'Upgrade to Donatico Pro', 'donatico' ),
				__( 'Upgrade to Pro ★', 'donatico' ),
				'manage_options',
				'donatico-upgrade',
				array( $this->upgradePage, 'render' )
			);
		}
	}

	/**
	 * Register the Settings submenu at a high priority so it appears
	 * below all other Donatico submenus (including Pro add-on pages).
	 * Hooked to admin_menu at priority 99.
	 *
	 * @return void
	 */
	public function registerSettingsMenu(): void {
		add_submenu_page(
			'donatico',
			__( 'Settings — Donatico', 'donatico' ),
			__( 'Settings', 'donatico' ),
			'manage_options',
			'donatico-settings',
			array( $this->settings, 'render' )
		);
	}

	/**
	 * Base64-encoded emerald SVG heart used as the menu icon.
	 *
	 * @return string
	 */
	private function getMenuIcon(): string {
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="#10b981">'
			. '<path fill-rule="evenodd" d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z" clip-rule="evenodd"/>'
			. '</svg>';

		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
	}
}
