<?php
/**
 * Admin: menu page, asset loading, dashboard shell.
 *
 * @package Defog
 */

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

class Defog_Admin {

	const HOOK = 'toplevel_page_defog';

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
	}

	/**
	 * Register the top-level menu.
	 */
	public function menu() {
		// No explicit position: the item goes to the bottom of the admin menu,
		// below all core items (wp.org review guidance — don't compete with core).
		add_menu_page(
			__( 'Fogless Analytics', 'fogless-analytics' ),
			__( 'Fogless', 'fogless-analytics' ),
			'manage_options',
			'defog',
			array( $this, 'render' ),
			'dashicons-chart-area'
		);
	}

	/**
	 * Load dashboard assets only on our page.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function assets( $hook ) {
		if ( self::HOOK !== $hook ) {
			return;
		}

		wp_enqueue_style( 'defog-admin', DEFOG_URL . 'assets/css/defog-admin.css', array(), DEFOG_VERSION );

		wp_enqueue_script( 'defog-admin', DEFOG_URL . 'assets/js/defog-admin.js', array(), DEFOG_VERSION, true );
		wp_localize_script(
			'defog-admin',
			'DefogAdmin',
			array(
				'rest'    => esc_url_raw( rest_url( 'defog/v1' ) ),
				'nonce'   => wp_create_nonce( 'wp_rest' ),
				'version' => DEFOG_VERSION,
				// Where "Missing something?" feedback is addressed. Change this to your
				// real support address (or hook the `defog_feedback_email` filter).
				'feedbackEmail' => apply_filters( 'defog_feedback_email', 'info@akoenterprise.cz' ),
				'i18n'    => $this->strings(),
			)
		);
	}

	/**
	 * Localised UI strings handed to the dashboard JS.
	 *
	 * @return array
	 */
	private function strings() {
		return array(
			'realTime'   => __( 'Real-time', 'fogless-analytics' ),

			// Section navigation (sticky top bar).
			'navOverview' => __( 'Overview', 'fogless-analytics' ),
			'navStore'    => __( 'Store', 'fogless-analytics' ),
			'navJourney'  => __( 'Journey', 'fogless-analytics' ),
			'navInsights' => __( 'Insights', 'fogless-analytics' ),

			// Dashboard guide ("How to read this").
			'guideShow'  => __( 'Guide', 'fogless-analytics' ),
			'guideTitle' => __( 'How to read this', 'fogless-analytics' ),
			'guideHide'  => __( 'Hide', 'fogless-analytics' ),
			'gRange'     => __( 'Switch the range — today, 7 or 30 days', 'fogless-analytics' ),
			'gClick'     => __( 'Click any page for a behaviour breakdown', 'fogless-analytics' ),
			'gHelp'      => __( 'Hover the ? on any card to learn the metric', 'fogless-analytics' ),
			'gEcom'      => __( 'Running a store? Revenue, funnel and products appear below', 'fogless-analytics' ),
			'gPrivacy'   => __( 'Cookieless — no IP stored, GDPR-friendly', 'fogless-analytics' ),

			'liveNote'   => __( 'Updates live · independent of the date range', 'fogless-analytics' ),
			'rightNow'   => __( 'visitors on your site right now', 'fogless-analytics' ),
			'last30'     => __( 'Last 30 minutes', 'fogless-analytics' ),
			'peak'       => __( 'peak', 'fogless-analytics' ),
			'activePages' => __( 'Active pages', 'fogless-analytics' ),
			'liveSources' => __( 'Live sources', 'fogless-analytics' ),
			'scope'      => __( 'The dashboard below reflects your selected range:', 'fogless-analytics' ),
			'today'      => __( 'Today', 'fogless-analytics' ),
			'd7'         => __( 'Last 7 days', 'fogless-analytics' ),
			'd30'        => __( 'Last 30 days', 'fogless-analytics' ),
			'visitors'   => __( 'Visitors', 'fogless-analytics' ),
			'pageviews'  => __( 'Pageviews', 'fogless-analytics' ),
			'sessions'   => __( 'Sessions', 'fogless-analytics' ),
			'bounce'     => __( 'Bounce rate', 'fogless-analytics' ),
			'traffic'    => __( 'Traffic over time', 'fogless-analytics' ),
			'thisPeriod' => __( 'This period', 'fogless-analytics' ),
			'prevPeriod' => __( 'Previous period', 'fogless-analytics' ),
			'acquisition' => __( 'Acquisition', 'fogless-analytics' ),
			'channels'   => __( 'Channels', 'fogless-analytics' ),
			'topPages'   => __( 'Top pages', 'fogless-analytics' ),
			'topCountries' => __( 'Top countries', 'fogless-analytics' ),
			'devices'    => __( 'Devices', 'fogless-analytics' ),
			'mobile'     => __( 'mobile', 'fogless-analytics' ),
			'gdpr'       => __( 'Cookieless · GDPR ready', 'fogless-analytics' ),
			'diviReady'  => __( 'Divi 5 ready', 'fogless-analytics' ),
			'updated'    => __( 'Updated just now · data stays on your server', 'fogless-analytics' ),
			/* translators: %d is a number of minutes. */
			'updatedMin' => __( 'Updated %d min ago · data stays on your server', 'fogless-analytics' ),
			/* translators: %d is a number of hours. */
			'updatedHr'  => __( 'Updated %d h ago · data stays on your server', 'fogless-analytics' ),
			'noData'     => __( 'No data yet — once visitors arrive, they show up here.', 'fogless-analytics' ),
			'emptyOrders' => __( 'This fills in with your first order.', 'fogless-analytics' ),
			'vs'         => __( 'vs', 'fogless-analytics' ),
			'ecommerce'  => __( 'E-commerce', 'fogless-analytics' ),
			'revenue'    => __( 'Revenue', 'fogless-analytics' ),
			'orders'     => __( 'Orders', 'fogless-analytics' ),
			'convRate'   => __( 'Conversion rate', 'fogless-analytics' ),
			'aov'        => __( 'Avg. order value', 'fogless-analytics' ),
			'sales'      => __( 'Sales over time', 'fogless-analytics' ),
			'revByChannel' => __( 'Revenue by channel', 'fogless-analytics' ),
			'attribution' => __( 'Attribution', 'fogless-analytics' ),
			'attribNote' => __( 'Attributed to the visitor\'s last source within the day (cookieless). Cross-day attribution is not in MVP.', 'fogless-analytics' ),
			'topProducts' => __( 'Top products', 'fogless-analytics' ),
			'unitsSold'  => __( 'units sold', 'fogless-analytics' ),
			'ordersShort' => __( 'orders', 'fogless-analytics' ),
			'points'     => __( 'pts', 'fogless-analytics' ),
			'funnelTitle' => __( 'Path to purchase', 'fogless-analytics' ),
			'funnelSub'  => __( 'How many visitors pass through each step toward an order', 'fogless-analytics' ),
			'overallConv' => __( 'overall conversion', 'fogless-analytics' ),
			'ofVisits'   => __( 'of visits', 'fogless-analytics' ),
			'continued'  => __( 'continued', 'fogless-analytics' ),
			'dropped'    => __( 'dropped', 'fogless-analytics' ),
			'fnVisitors' => __( 'Visitors', 'fogless-analytics' ),
			'fnProduct'  => __( 'Product view', 'fogless-analytics' ),
			'fnCart'     => __( 'Add to cart', 'fogless-analytics' ),
			'fnCheckout' => __( 'Checkout', 'fogless-analytics' ),
			'fnPurchase' => __( 'Purchase', 'fogless-analytics' ),
			'pageDetail' => __( 'Page detail', 'fogless-analytics' ),
			'clickHint'  => __( 'click a page for behaviour detail', 'fogless-analytics' ),
			'dViews'     => __( 'Pageviews', 'fogless-analytics' ),
			'dVisitors'  => __( 'Unique visitors', 'fogless-analytics' ),
			'dTime'      => __( 'Avg. time on page', 'fogless-analytics' ),
			'dBounce'    => __( 'Bounce rate', 'fogless-analytics' ),
			'dEntry'     => __( 'Entry rate', 'fogless-analytics' ),
			'dExit'      => __( 'Exit rate', 'fogless-analytics' ),
			'dViewsTime' => __( 'Views over time', 'fogless-analytics' ),
			'dSources'   => __( 'Traffic sources for this page', 'fogless-analytics' ),
			'dDevices'   => __( 'Devices', 'fogless-analytics' ),
			'close'      => __( 'Close', 'fogless-analytics' ),

			// Contextual help (? tooltips).
			'hVisitors'  => __( 'Unique people over the whole period — each visitor counted once, even across several days. May be lower than the sum of the daily bars in the chart. Cookieless, no stored IP.', 'fogless-analytics' ),
			'hPageviews' => __( 'Total pages loaded. A single visitor can generate many pageviews.', 'fogless-analytics' ),
			'hSessions'  => __( 'Visits. A session groups a visitor’s activity; a new one starts after a break or on a new day.', 'fogless-analytics' ),
			'hBounce'    => __( 'Share of sessions with just one pageview and no real engagement. Lower is usually better.', 'fogless-analytics' ),
			'hChannels'  => __( 'Where your visitors came from — organic search, direct, social, referral and paid ads.', 'fogless-analytics' ),
			'hTopPages'  => __( 'Your most-viewed pages in this period. Click any row for a behaviour breakdown.', 'fogless-analytics' ),
			'hCountries' => __( 'Visitor countries, detected from network edge headers. No IP address is stored.', 'fogless-analytics' ),
			'hDevices'   => __( 'Split of visits by device type — mobile, desktop or tablet.', 'fogless-analytics' ),
			'hTraffic'   => __( 'Trend across the selected period. The dashed line is the previous period, for comparison.', 'fogless-analytics' ),

			// Feedback ("Missing something?").
			'fbTrigger'    => __( 'Missing something? Let us know', 'fogless-analytics' ),
			'fbShort'      => __( 'Feedback', 'fogless-analytics' ),
			'fbTitle'      => __( 'Tell us what’s missing', 'fogless-analytics' ),
			'fbLead'       => __( 'Which metric or feature would make Defog more useful for you? Your note helps us decide what to build next.', 'fogless-analytics' ),
			'fbPlaceholder' => __( 'e.g. I’d love to track form submissions and button clicks…', 'fogless-analytics' ),
			'fbSend'       => __( 'Open in email', 'fogless-analytics' ),
			'fbNote'       => __( 'This opens your email app with the message pre-filled — nothing is sent automatically.', 'fogless-analytics' ),
			'fbSubject'    => __( 'Fogless Analytics — feature feedback', 'fogless-analytics' ),

			// Events / actions card.
			'evTitle'    => __( 'Events', 'fogless-analytics' ),
			'evEyebrow'  => __( 'Interactions', 'fogless-analytics' ),
			'evOutbound' => __( 'Outbound clicks', 'fogless-analytics' ),
			'evDownloads' => __( 'Downloads', 'fogless-analytics' ),
			'evForms'    => __( 'Form submits', 'fogless-analytics' ),
			'evClicks'   => __( 'clicks', 'fogless-analytics' ),
			'evSubmits'  => __( 'submits', 'fogless-analytics' ),
			'evPeople'   => __( 'people', 'fogless-analytics' ),
			'hEvents'    => __( 'Actions visitors take on your site: outbound clicks (links to other websites), file downloads and form submissions. Only the fact of the action is stored — never any form content.', 'fogless-analytics' ),


			// Customer journey / user flow.
			'jTitle'     => __( 'Customer Journey', 'fogless-analytics' ),
			'jEyebrow'   => __( 'User flow', 'fogless-analytics' ),
			'jSub'       => __( 'How visitors move through your site, step by step', 'fogless-analytics' ),
			/* translators: %d is the step number. */
			'jStep'      => __( 'Step %d', 'fogless-analytics' ),
			'jEntry'     => __( 'Entry', 'fogless-analytics' ),
			'jSessions'  => __( 'journeys', 'fogless-analytics' ),
			'hJourney'   => __( 'Reconstructed from real pageview order: the top pages at each step of a visit and how people flow between them. Cookieless, from your own data.', 'fogless-analytics' ),

			// Insights / recommendations.
			'insTitle'   => __( 'Insights', 'fogless-analytics' ),
			'insEyebrow' => __( 'Recommendations', 'fogless-analytics' ),
			'insSub'     => __( 'What your data suggests you do next', 'fogless-analytics' ),
			'hInsights'  => __( 'Automatic tips tailored to your site type (store, lead-gen or content) — trends, bounce, channel mix, funnel friction, form performance and more. Guidance, not gospel: treat them as prompts.', 'fogless-analytics' ),
		);
	}

	/**
	 * Render the dashboard mount point. The JS builds the UI and fetches data.
	 */
	public function render() {
		echo '<div class="wrap"><div id="defog-app" data-defog></div></div>';
	}
}
