<?php
defined( 'ABSPATH' ) || exit;

add_action( 'admin_menu', 'formulate_pricing_register_menu' );

function formulate_pricing_register_menu() {
	// ── Top-level page ────────────────────────────────────────────────────────
	add_menu_page(
		__( 'Formulate Pricing', 'formulate-pricing' ),
		__( 'Formulate Pricing', 'formulate-pricing' ),
		'manage_options',
		'formulate-pricing',
		'formulate_pricing_forms_list_page',
		'dashicons-tag',
		30
	);

	// All Forms (same as parent)
	add_submenu_page(
		'formulate-pricing',
		__( 'All Forms', 'formulate-pricing' ),
		__( 'All Forms', 'formulate-pricing' ),
		'manage_options',
		'formulate-pricing',
		'formulate_pricing_forms_list_page'
	);

	// Add New Form
	add_submenu_page(
		'formulate-pricing',
		__( 'Add New Form', 'formulate-pricing' ),
		__( 'Add New', 'formulate-pricing' ),
		'manage_options',
		'formulate-pricing-new',
		'formulate_pricing_builder_page'
	);

	// Entries
	add_submenu_page(
		'formulate-pricing',
		__( 'Entries', 'formulate-pricing' ),
		__( 'Entries', 'formulate-pricing' ),
		'manage_options',
		'formulate-pricing-entries',
		'formulate_pricing_entries_page'
	);

}

// ── Enqueue shared admin CSS on plugin pages (not on builder) ─────────────────
add_action( 'admin_enqueue_scripts', function () {
	$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( ! $page || strpos( $page, 'formulate-pricing' ) !== 0 ) {
		return;
	}

	// HugeIcons – loaded on all plugin pages (builder + admin)
	wp_enqueue_style(
		'hugeicons',
		FORMULATE_PRICING_URL . 'assets/vendor/hugeicons/hgi-stroke-rounded.css',
		array(),
		FORMULATE_PRICING_VERSION
	);

	// Builder has its own CSS – skip admin.css but still load HugeIcons above
	if ( in_array( $page, array( 'formulate-pricing-new', 'formulate-pricing-edit' ), true ) ) {
		return;
	}

	wp_enqueue_style(
		'formulate-pricing-admin',
		FORMULATE_PRICING_URL . 'assets/css/admin.css',
		array( 'hugeicons' ),
		FORMULATE_PRICING_VERSION
	);

	// Shared keyframe animation used across admin pages.
	wp_add_inline_style( 'formulate-pricing-admin', '@keyframes bf-spin { to { transform: rotate(360deg); } }' );

	// ── Entries page ───────────────────────────────────────────────────────
	if ( 'formulate-pricing-entries' === $page ) {
		wp_enqueue_script(
			'formulate-pricing-admin-entries',
			FORMULATE_PRICING_URL . 'assets/js/admin-entries.js',
			array( 'jquery' ),
			FORMULATE_PRICING_VERSION,
			true
		);
		wp_localize_script(
			'formulate-pricing-admin-entries',
			'formulatePricingEntries',
			array(
				'nonce'   => wp_create_nonce( 'formulate_pricing_admin_nonce' ),
				'strings' => array(
					'loading'           => __( 'Loading…', 'formulate-pricing' ),
					'id'                => __( 'ID', 'formulate-pricing' ),
					'date'              => __( 'Date', 'formulate-pricing' ),
					'status'            => __( 'Status', 'formulate-pricing' ),
					'payment'           => __( 'Payment', 'formulate-pricing' ),
					'order'             => __( 'Order', 'formulate-pricing' ),
					'fieldLabel'        => __( 'Field', 'formulate-pricing' ),
					'confirmDelete'     => __( 'Delete this entry? This cannot be undone.', 'formulate-pricing' ),
					'selectAction'      => __( 'Please select an action.', 'formulate-pricing' ),
					'selectEntries'     => __( 'Please select at least one entry.', 'formulate-pricing' ),
					'confirmBulkDelete' => __( 'Permanently delete the selected entries?', 'formulate-pricing' ),
				),
			)
		);
	}

	// ── Forms list page ────────────────────────────────────────────────────
	if ( 'formulate-pricing' === $page ) {
		wp_enqueue_script(
			'formulate-pricing-admin-forms',
			FORMULATE_PRICING_URL . 'assets/js/admin-forms-list.js',
			array( 'jquery' ),
			FORMULATE_PRICING_VERSION,
			true
		);
		wp_localize_script(
			'formulate-pricing-admin-forms',
			'formulatePricingForms',
			array(
				'editBaseUrl' => admin_url( 'admin.php?page=formulate-pricing-edit&form_id=' ),
				'strings'     => array(
					'duplicating'   => __( 'Duplicating…', 'formulate-pricing' ),
					'duplicate'     => __( 'Duplicate', 'formulate-pricing' ),
					'errorOccurred' => __( 'An error occurred.', 'formulate-pricing' ),
					'confirmDelete' => __( 'Delete this form and all its entries? This cannot be undone.', 'formulate-pricing' ),
				),
			)
		);
	}
} );

// ── Add body class on builder pages so CSS can hide WP sidebar & footer ───────
add_filter( 'admin_body_class', function ( $classes ) {
	$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( ! $page || strpos( $page, 'formulate-pricing' ) !== 0 ) {
		return $classes;
	}
	if ( in_array( $page, array( 'formulate-pricing-new', 'formulate-pricing-edit' ), true ) ) {
		$classes .= ' bf-builder-fullscreen';
	} else {
		$classes .= ' bf-admin-page';
	}
	return $classes;
} );

// ── Hidden "edit form" page (not in menu) ─────────────────────────────────────
add_action( 'admin_menu', 'formulate_pricing_register_hidden_pages', 20 );

function formulate_pricing_register_hidden_pages() {
	add_submenu_page(
		null,
		__( 'Edit Form', 'formulate-pricing' ),
		'',
		'manage_options',
		'formulate-pricing-edit',
		'formulate_pricing_builder_page'
	);
}

// ── Page handler functions must be available when the menu callbacks fire ─────
if ( is_admin() ) {
	require_once FORMULATE_PRICING_PATH . 'admin/forms-list-page.php';
	require_once FORMULATE_PRICING_PATH . 'admin/form-builder-page.php';
	require_once FORMULATE_PRICING_PATH . 'admin/entries-page.php';
}

// ── Highlight correct menu item when editing a form ───────────────────────────
add_filter(
	'parent_file',
	function ( $parent_file ) {
		$screen = get_current_screen();
		if ( $screen && 'admin_page_formulate-pricing-edit' === $screen->id ) {
			return 'formulate-pricing';
		}
		return $parent_file;
	}
);

add_filter(
	'submenu_file',
	function ( $submenu_file ) {
		$screen = get_current_screen();
		if ( $screen && 'admin_page_formulate-pricing-edit' === $screen->id ) {
			return 'formulate-pricing';
		}
		return $submenu_file;
	}
);
