<?php
/**
 * Top-level admin menu + submenu pages.
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Admin;

use SuiteMkt3k\Security\Capabilities;
use SuiteMkt3k\Admin\Pages\Dashboard_Page;
use SuiteMkt3k\Admin\Pages\Coupons_Page;
use SuiteMkt3k\Admin\Pages\Newsletter_Page;
use SuiteMkt3k\Admin\Pages\Cart_Recovery_Page;
use SuiteMkt3k\Admin\Pages\Subscribers_Page;
use SuiteMkt3k\Admin\Pages\Statistics_Page;
use SuiteMkt3k\Admin\Pages\Settings_Page;
use SuiteMkt3k\Admin\Pages\Help_Page;
use SuiteMkt3k\Admin\Pages\Activity_Page;

defined( 'ABSPATH' ) || exit;

final class Admin_Menu {

	public const SLUG = 'suitemkt3k';

	public static function register(): void {
		add_action( 'admin_menu', array( __CLASS__, 'register_menus' ), 9 );
	}

	public static function register_menus(): void {
		$cap   = Capabilities::MANAGE;
		$icon  = self::icon_data_uri();
		$pages = self::pages();

		add_menu_page(
			__( 'SuiteMkt3k', 'suitemkt3k' ),
			__( 'SuiteMkt3k', 'suitemkt3k' ),
			$cap,
			self::SLUG,
			array( Dashboard_Page::class, 'render' ),
			$icon,
			58
		);

		foreach ( $pages as $page ) {
			// Hidden pages: parent null registers the URL without a submenu entry
			// (remove_submenu_page breaks direct access to admin.php?page=…).
			$parent = ! empty( $page['hidden'] ) ? null : self::SLUG;

			add_submenu_page(
				$parent,
				$page['title'],
				$page['label'],
				$page['cap'],
				$page['slug'],
				$page['callback']
			);
		}
	}

	public static function pages(): array {
		return array(
			array(
				'slug'     => self::SLUG,
				'label'    => __( 'Dashboard', 'suitemkt3k' ),
				'title'    => __( 'SuiteMkt3k Dashboard', 'suitemkt3k' ),
				'cap'      => Capabilities::MANAGE,
				'icon'     => 'home',
				'callback' => array( Dashboard_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-coupons',
				'label'    => __( 'Coupons', 'suitemkt3k' ),
				'title'    => __( 'Coupon campaigns', 'suitemkt3k' ),
				'cap'      => Capabilities::EDIT_CAMPAIGN,
				'icon'     => 'ticket',
				'callback' => array( Coupons_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-newsletter',
				'label'    => __( 'Newsletter', 'suitemkt3k' ),
				'title'    => __( 'Newsletter campaigns', 'suitemkt3k' ),
				'cap'      => Capabilities::EDIT_CAMPAIGN,
				'icon'     => 'mail',
				'callback' => array( Newsletter_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-cart-recovery',
				'label'    => __( 'Abandoned Carts', 'suitemkt3k' ),
				'title'    => __( 'Abandoned Cart Recovery', 'suitemkt3k' ),
				'cap'      => Capabilities::EDIT_CAMPAIGN,
				'icon'     => 'cart',
				'callback' => array( Cart_Recovery_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-subscribers',
				'label'    => __( 'Subscribers', 'suitemkt3k' ),
				'title'    => __( 'Subscribers', 'suitemkt3k' ),
				'cap'      => Capabilities::EDIT_CAMPAIGN,
				'icon'     => 'users',
				'callback' => array( Subscribers_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-statistics',
				'label'    => __( 'Statistics', 'suitemkt3k' ),
				'title'    => __( 'Statistics', 'suitemkt3k' ),
				'cap'      => Capabilities::VIEW_STATS,
				'icon'     => 'chart',
				'callback' => array( Statistics_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-settings',
				'label'    => __( 'Settings', 'suitemkt3k' ),
				'title'    => __( 'Settings', 'suitemkt3k' ),
				'cap'      => Capabilities::EDIT_SETTINGS,
				'icon'     => 'settings',
				'callback' => array( Settings_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-help',
				'label'    => __( 'Help', 'suitemkt3k' ),
				'title'    => __( 'Help & documentation', 'suitemkt3k' ),
				'cap'      => Capabilities::MANAGE,
				'icon'     => 'help',
				'callback' => array( Help_Page::class, 'render' ),
			),
			array(
				'slug'     => 'suitemkt3k-activity',
				'label'    => __( 'Activity log', 'suitemkt3k' ),
				'title'    => __( 'Activity log', 'suitemkt3k' ),
				'cap'      => Capabilities::MANAGE,
				'icon'     => 'chart',
				'callback' => array( Activity_Page::class, 'render' ),
				'hidden'   => true,
			),
		);
	}

	private static function icon_data_uri(): string {
		$svg = '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path fill="#a7aaad" d="M4 5h16a1 1 0 0 1 1 1v8a3 3 0 0 1-3 3h-2l-3 3v-3H6a3 3 0 0 1-3-3V6a1 1 0 0 1 1-1zm3 4v2h10V9H7zm0 4v2h6v-2H7z"/></svg>';
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
	}
}
