<?php
/**
 * Admin Assets
 *
 * Enqueues admin CSS and JS conditionally — only on Relatedly pages,
 * and statistics-only assets only on the statistics page.
 * All libraries are bundled locally via webpack (no CDN URLs).
 *
 * @package Relatedly\Admin
 * @since   2.0.0
 */

namespace Relatedly\Admin;

use Relatedly\Core\Options;
use Relatedly\Premium\FeatureGate;

defined( 'ABSPATH' ) || exit;

/**
 * Enqueues scripts and styles for Relatedly admin pages.
 *
 * @since 2.0.0
 */
class Assets {

	/**
	 * Plugin settings repository.
	 *
	 * @since 2.0.0
	 * @var Options
	 */
	private Options $options;

	/**
	 * Create the admin assets service.
	 *
	 * @since 2.0.0
	 * @param Options $options Plugin settings repository.
	 */
	public function __construct( Options $options ) {
		$this->options = $options;
	}

	/**
	 * Register admin enqueue hooks.
	 *
	 * @since 2.0.0
	 */
	public function init_hooks(): void {
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ] );
	}

	/**
	 * Enqueue admin assets for Relatedly pages.
	 *
	 * @since 2.0.0
	 * @param string $hook Current admin page hook suffix.
	 */
	public function enqueue( string $hook ): void {
		$relatedly_pages = [
			'toplevel_page_relatedly',
			'relatedly_page_relatedly-statistics',
		];

		if ( ! in_array( $hook, $relatedly_pages, true ) ) {
			return;
		}

		// Vendor bundle — Choices.js, Tippy.js, Popper.js, SweetAlert2 (built by webpack).
		wp_enqueue_script(
			'relatedly-vendor',
			RELATEDLY_PLUGIN_URL . 'assets/vendor/vendor.js',
			[ 'jquery' ],
			RELATEDLY_VERSION,
			true
		);
		wp_enqueue_style(
			'relatedly-vendor',
			RELATEDLY_PLUGIN_URL . 'assets/vendor/vendor.css',
			[],
			RELATEDLY_VERSION
		);

		$admin_css_path    = RELATEDLY_PLUGIN_DIR . 'assets/css/admin.css';
		$admin_css_version = file_exists( $admin_css_path ) ? (string) filemtime( $admin_css_path ) : RELATEDLY_VERSION;

		// Admin stylesheet.
		wp_enqueue_style(
			'relatedly-admin',
			RELATEDLY_PLUGIN_URL . 'assets/css/admin.css',
			[ 'relatedly-vendor' ],
			$admin_css_version
		);

		// Settings page JS — only on the settings page.
		if ( 'toplevel_page_relatedly' === $hook ) {
			wp_enqueue_style( 'wp-color-picker' );
			wp_enqueue_script( 'wp-color-picker' );

			wp_enqueue_script(
				'relatedly-admin-settings',
				RELATEDLY_PLUGIN_URL . 'assets/js/admin/settings.js',
				[ 'jquery', 'wp-color-picker', 'relatedly-vendor' ],
				RELATEDLY_VERSION,
				true
			);

			wp_localize_script(
				'relatedly-admin-settings',
				'relatedlyAdmin',
				[
					'ajaxUrl' => admin_url( 'admin-ajax.php' ),
					'nonce'   => wp_create_nonce( 'relatedly_settings' ),
					'strings' => [
						'active'   => __( 'Active', 'relatedly' ),
						'inactive' => __( 'Inactive', 'relatedly' ),
					],
				]
			);
		}

		// Statistics page JS — Chart.js, DataTables, Flatpickr bundled separately.
		if ( 'relatedly_page_relatedly-statistics' === $hook && FeatureGate::can_use_statistics_dashboard() ) {
			wp_enqueue_script(
				'relatedly-admin-statistics',
				RELATEDLY_PLUGIN_URL . 'assets/js/admin/statistics.js',
				[ 'jquery', 'relatedly-vendor' ],
				RELATEDLY_VERSION,
				true
			);

			wp_localize_script(
				'relatedly-admin-statistics',
				'relatedlyStats',
				[
					'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
					'nonce'        => wp_create_nonce( 'relatedly_statistics' ),
					'defaultStart' => gmdate( 'Y-m-d', strtotime( '-30 days' ) ),
					'defaultEnd'   => gmdate( 'Y-m-d' ),
					'editPostUrl'  => admin_url( 'post.php?action=edit&post=POST_ID' ),
					'strings'      => [
						'attention'       => __( 'Attention', 'relatedly' ),
						'invalidRange'    => __( 'Please select a valid date range.', 'relatedly' ),
						'comparing'       => __( 'Comparing Periods', 'relatedly' ),
						'pleaseWait'      => __( 'Please wait while we analyze the data.', 'relatedly' ),
						'metric'          => __( 'Metric', 'relatedly' ),
						'difference'      => __( 'Difference', 'relatedly' ),
						'clicks'          => __( 'Clicks', 'relatedly' ),
						'impressions'     => __( 'Impressions', 'relatedly' ),
						'ctr'             => __( 'CTR', 'relatedly' ),
						'view'            => __( 'View', 'relatedly' ),
						'analyticsTitle'  => __( 'Post Analytics', 'relatedly' ),
						'asClicked'       => __( 'As Clicked Post', 'relatedly' ),
						'asSource'        => __( 'As Source Post', 'relatedly' ),
						'clicksGenerated' => __( 'Clicks Generated', 'relatedly' ),
						'topRelated'      => __( 'Top Related Posts Clicked', 'relatedly' ),
						'post'            => __( 'Post', 'relatedly' ),
						'loading'         => __( 'Loading…', 'relatedly' ),
					],
				]
			);
		}
	}
}
