<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Template/class file; variables are widget-scoped, not true globals.

/**
 * storzen — Admin Panel (powered by bundled Storzen framework)
 *
 * Panel slug : storzen
 * Option keys: pk_storzen_{menu}_{tab}
 *
 * FREE  : Shop menu → Shop tab (basic widget toggles)
 * PRO   : Everything else — requires active license to use
 *
 * @package storzen
 * @since   1.0.3
 */

if (! defined('ABSPATH')) {
	exit;
}

use Storzen\Panel;
use Storzen\Menu;
use Storzen\Tab;
use Storzen\Section;
use Storzen\Fields\Toggle;
use Storzen\Fields\Text;
use Storzen\Fields\Number;
use Storzen\Fields\Url;
use Storzen\Fields\Email;
use Storzen\Fields\Select;
use Storzen\Fields\Radio;
use Storzen\Fields\Textarea;
use Storzen\Fields\Color;
use Storzen\Fields\Notice;

// ── Elementor template choices (lazy — only queries DB on render) ─────────────
$tpls = function () {
	$opts  = array('' => __('No Template', 'storzen'));
	$posts = get_posts(array(
		'post_type'      => 'elementor_library',
		'posts_per_page' => -1,
		'post_status'    => 'publish',
	));
	foreach ($posts as $p) {
		$opts[$p->ID] = $p->post_title;
	}
	return $opts;
};

// ─────────────────────────────────────────────────────────────────────────────
$panel = Panel::make('storzen', 'Storzen')
	->set_icon('dashicons-cart')
	->set_position(56);

// Only show PRO banner if not using PRO version
if (! defined('STORZEN_IS_PRO') || STORZEN_IS_PRO !== 'yes') {
	$panel->set_banner(
		__('✦ Unlock all PRO widgets & modules — more layouts, Ajax filters, and priority support.', 'storzen'),
		'https://storzen.webtend.net/pricing/',
		__('Go Pro →', 'storzen')
	);
}

$panel
	// ═══════════════════════════════════════════════════════════════════════
	// MENU 1 — Widgets  (Shop tab = FREE; all other tabs = PRO)
	// ═══════════════════════════════════════════════════════════════════════
	->add_menu(
		Menu::make('widgets', __('Elementor Widget', 'storzen'))->set_icon('🛍️')

			// ── Tab: Shop — FREE ───────────────────────────────────────────
			->add_tab(
				Tab::make('shop', __('Shop Widget', 'storzen'))
					->set_icon('🛍️')
					->set_description(__('Enable or disable shop page widgets.', 'storzen'))
					->add_section(
						Section::make('shop_widgets', __('Shop Widgets', 'storzen'))
							->layout('grid')->columns(4)
							->add_field(Toggle::make('product_card',         __('Product Card',          'storzen'))->set_default(true))
							->add_field(Toggle::make('archive_grid',         __('Archive Products Grid', 'storzen'))->set_default(true))
							->add_field(Toggle::make('sidebar_search',       __('Sidebar Products Search', 'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('sidebar_ajax_cats',    __('Sidebar Ajax Categories', 'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('sidebar_ajax_attrs',   __('Sidebar Ajax Attributes', 'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('sidebar_ajax_stock',   __('Sidebar Ajax Stock',    'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('sidebar_ajax_ratings', __('Sidebar Ajax Ratings',  'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('product_carousel', __('Product Carousel', 'storzen'))->set_description(__('Elementor Slider widget (carousel layouts).', 'storzen'))->set_default(true))
							->add_field(Toggle::make('product_grid_w', __('Product Grid', 'storzen'))->set_description(__('Elementor General Style widget (grid / landing layouts).', 'storzen'))->set_default(true))
							->add_field(Toggle::make('elementor_shop_tab', __('Tab Style', 'storzen'))->set_description(__('Elementor Tab widget (category tabs).', 'storzen'))->set_default(true))
							->add_field(Toggle::make('elementor_shop_list', __('List Style', 'storzen'))->set_description(__('Elementor List widget.', 'storzen'))->set_default(true))
							->add_field(Toggle::make('elementor_shop_sidebar', __('Shop With Sidebar', 'storzen'))->set_description(__('Elementor shop grid with filters sidebar.', 'storzen'))->set_default(true))
							->add_field(Toggle::make('archive_ordering',     __('Archive Ordering',     'storzen'))->set_default(true))
							->add_field(Toggle::make('archive_result_count', __('Archive Result Count', 'storzen'))->set_default(true))
							->add_field(Toggle::make('archive_title',        __('Archive Title',        'storzen'))->set_default(true))
					)
			)

			// ── Tab: Shop Details — PRO ────────────────────────────────────
			->add_tab(
				Tab::make('shop_details', __('Shop Details Widget', 'storzen'))
					->set_icon('📦')
					->set_description(__('Enable or disable single product page widgets.', 'storzen'))
					->add_section(
						Section::make('product_widgets', __('Product Widgets', 'storzen'))
							->layout('grid')->columns(4)
							->add_field(Toggle::make('product_title',   __('Product Title',    'storzen'))->set_default(true))
							->add_field(Toggle::make('product_price',   __('Product Price',    'storzen'))->set_default(true))
							->add_field(Toggle::make('product_rating',  __('Product Rating',   'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('product_atc',     __('Add to Cart',      'storzen'))->set_default(true))
							->add_field(Toggle::make('product_gallery', __('Product Gallery',  'storzen'))->set_default(true))
							->add_field(Toggle::make('product_desc',    __('Product Description', 'storzen'))->set_default(true))
							->add_field(Toggle::make('related_products', __('Related Products', 'storzen'))->set_default(true))
							->add_field(Toggle::make('product_tabs',    __('Product Tabs',     'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('sticky_atc',      __('Sticky Add to Cart', 'storzen'))->set_default(false))
							->add_field(Toggle::make('product_share',   __('Product Share',    'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('product_meta',    __('SKU, Category & Tags', 'storzen'))->set_default(true))
							->add_field(Toggle::make('product_service_info', __('Product Service Info', 'storzen'))->set_description(__('Sidebar: shipping, support, returns.', 'storzen'))->set_default(true))
							->add_field(Toggle::make('buy_now',         __('Buy It Now',       'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('product_utility_links', __('Compare / Shipping / Ask', 'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('realtime_visitor', __('Realtime Visitor', 'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('product_reviews',      __('Product Reviews',  'storzen'))->set_default(true))
							->add_field(Toggle::make('product_stock_widget', __('Product Stock',    'storzen'))->set_default(true))
							->add_field(Toggle::make('product_upsells',      __('Upsell Products',   'storzen'))->set_default(true))
							->add_field(Toggle::make('product_breadcrumbs',  __('Product Breadcrumbs','storzen'))->set_default(true))
					)
			)

			// ── Tab: Cart — PRO ────────────────────────────────────────────
			->add_tab(
				Tab::make('cart', __('Cart Widget', 'storzen'))
					->set_icon('🛒')
					->add_section(
						Section::make('cart_widgets', __('Cart Widgets', 'storzen'))
							->layout('grid')->columns(4)
							->add_field(Toggle::make('cart_table',  __('Cart Table',      'storzen'))->set_default(true))
							->add_field(Toggle::make('cart_totals', __('Cart Totals',     'storzen'))->set_default(true))
							->add_field(Toggle::make('cart_coupon',           __('Cart Coupon',       'storzen'))->set_default(true))
							->add_field(Toggle::make('cart_empty_message',     __('Empty Cart Message', 'storzen'))->set_default(true))
							->add_field(Toggle::make('cart_cross_sell_widget', __('Cross-Sell Products','storzen'))->set_default(true))
					)
			)

			// ── Tab: Checkout — PRO ────────────────────────────────────────
			->add_tab(
				Tab::make('checkout', __('Checkout Widget', 'storzen'))
					->set_icon('💳')
					->add_section(
						Section::make('checkout_widgets', __('Checkout Widgets', 'storzen'))
							->layout('grid')->columns(4)
							->add_field(Toggle::make('checkout_form',     __('Checkout Form',    'storzen'))->set_default(true))
							->add_field(Toggle::make('order_review',      __('Order Review',     'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('payment_methods',     __('Payment Methods',  'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('checkout_trust_badges', __('Trust Badges', 'storzen'))->set_default(true))
					)

			)

			// ── Tab: Thank You — PRO ───────────────────────────────────────
			->add_tab(
				Tab::make('thankyou', __('Thank You Widget', 'storzen'))
					->set_icon('✅')
					->set_pro()
					->add_section(
						Section::make('ty_widgets', __('Thank You Widgets', 'storzen'))
							->layout('grid')->columns(4)
							->add_field(Toggle::make('order_details', __('Order Details',        'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('order_summary', __('Order Summary',        'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('upsell_after',  __('Upsell After Purchase', 'storzen'))->set_default(false)->set_pro())
					)
			)

			// ── Tab: My Account — PRO ──────────────────────────────────────
			->add_tab(
				Tab::make('my_account', __('My Account Widget', 'storzen'))
					->set_icon('👤')
					->set_pro()
					->add_section(
						Section::make('acc_widgets', __('My Account Widgets', 'storzen'))
							->layout('grid')->columns(4)
							->add_field(Toggle::make('acc_dashboard', __('Account Dashboard', 'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('acc_orders',    __('Account Orders',    'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('acc_addresses', __('Account Addresses', 'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('acc_details',   __('Account Details',   'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('acc_login',     __('Account Login',     'storzen'))->set_default(true)->set_pro())
							->add_field(Toggle::make('acc_wishlist',  __('Account Wishlist',  'storzen'))->set_default(false)->set_pro())
							->add_field(Toggle::make('acc_full',      __('My Account (Full with Tabs)', 'storzen'))->set_description(__('Complete account page with navigation tabs.', 'storzen'))->set_default(true)->set_pro())
					)
			)

	)

	// ═══════════════════════════════════════════════════════════════════════
	// MENU 2 — Templates — PRO
	// ═══════════════════════════════════════════════════════════════════════
	->add_menu(
		Menu::make('templates', __('Page Templates', 'storzen'))->set_icon('🗂️')
			->add_tab(
				Tab::make('page_templates', __('Page Templates', 'storzen'))
					->set_icon('🗂️')
					->set_description(__('Assign Elementor templates to WooCommerce pages.', 'storzen'))
					->add_section(
						Section::make('page_tpls', __('Page Templates', 'storzen'))
							->set_description(__('Select a saved Elementor template for each store page.', 'storzen'))
							->add_field(Select::make('shop_tpl',      __('Shop Page Template',       'storzen'))->set_options($tpls)->set_default(''))
							->add_field(Select::make('single_tpl',    __('Single Product Template',  'storzen'))->set_options($tpls)->set_default(''))
							->add_field(Select::make('cart_tpl',      __('Cart Page Template',       'storzen'))->set_options($tpls)->set_default(''))
							->add_field(Select::make('checkout_tpl',  __('Checkout Page Template',   'storzen'))->set_options($tpls)->set_default(''))
							->add_field(Select::make('thankyou_tpl',  __('Thank You Page Template',  'storzen'))->set_options($tpls)->set_default('')->set_pro())
							->add_field(Select::make('myaccount_tpl', __('My Account Page Template', 'storzen'))->set_options($tpls)->set_default('')->set_pro())
					)
			)
	)

	// NOTE: The Cart menu was removed — its settings (Ajax Add to Cart,
	// Redirect, Mini Cart Drawer) plus the Cross Sell Popup now live in the
	// "Cart" module on the Modules page (Storzen → Modules).

	->register();

// Expose the Panel instance as a global so Storzen_Modules_Admin_Page can
// render the same top nav (Widgets / Cart / License / Modules) on its page.
// Storzen_Modules_Admin_Page::get_panel_nav_html() reads this exact global.
global $storzen_panel_instance; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$storzen_panel_instance = $panel;
