<?php

namespace HulkPlugins\Woo\AccountPages\Admin\Controllers;

use HulkPlugins\Woo\AccountPages\Admin\Services\AccountPageMetaBoxService;
use HulkPlugins\Woo\AccountPages\Admin\Services\AccountPagesService;
use HulkPlugins\Woo\AccountPages\Core\BaseController;
use HulkPlugins\Woo\AccountPages\Core\SingletonTrait;
use WP_Post;
use const HulkPlugins\Woo\AccountPages\PLUGIN_DIR_PATH;

defined( 'ABSPATH' ) || exit;

/**
 * Classic editor compatibility meta
 */
class AccountPageMetaBoxClassicController extends BaseController {

	use SingletonTrait;

	protected AccountPagesService $pages_service;

	public function init_hooks(): void {
		$this->pages_service = AccountPagesService::getInstance();

		add_action( "add_meta_boxes_{$this->pages_service->post_type}", [ $this, 'add_meta_boxes' ] );
		add_action( "save_post_{$this->pages_service->post_type}", [ $this, 'save_meta_boxes' ], 10, 2 );
	}

	/**
	 * Add meta box to the sidebar for classic editor only
	 */
	public function add_meta_boxes(): void {

		if ( use_block_editor_for_post_type( $this->pages_service->post_type ) ) {
			return; // Gutenberg active → don't add meta box
		}

		// Add meta box for classic editor only
		add_meta_box(
			'hulk_woo_ap_meta_box',
			esc_attr__( 'Account Page Settings', 'hulk-account-pages-for-woocommerce' ),
			[ $this, 'account_page_settings_meta_box' ],
			$this->pages_service->post_type,
			'side',
			'high',
		);
	}

	/**
	 * Render meta box HTML on classic editor only
	 */
	public function account_page_settings_meta_box( $post ): void {

		$metaService = AccountPageMetaBoxService::getInstance();

		/** @var WP_Post $post */
		$system_roles = $metaService->get_system_roles();
		$meta_roles = $metaService->get_allowed_user_roles( $post->ID );

		wp_nonce_field( 'account_page_meta_action', 'account_page_meta_nonce' );

		include PLUGIN_DIR_PATH . '/templates/admin/account-page-meta-box.php';
	}

	/**
	 * Save roles meta data
	 */
	public function save_meta_boxes( $post_id, $post ): void {

		// Ignore autosaves/revisions
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		if ( wp_is_post_revision( $post_id ) ) {
			return;
		}

		// If this is a REST request (Gutenberg), do nothing
		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			return;
		}

		// Check post type (extra safety)
		if ( $post->post_type !== $this->pages_service->post_type ) {
			return;
		}

		// Check nonce
		if (
			! isset( $_POST['account_page_meta_nonce'] ) ||
			! wp_verify_nonce( wp_unslash( $_POST['account_page_meta_nonce'] ), 'account_page_meta_action' )
		) {
			return;
		}

		// 4. Check user capability
		if ( ! current_user_can( 'edit_post', $post_id ) ) {
			return;
		}

		// 5. Now safe to save
		if ( isset( $_POST['hulk_woo_ap_user_roles'] ) ) {
			update_post_meta(
				$post_id,
				'hulk_woo_ap_user_roles',
				array_map( 'sanitize_text_field', wp_unslash( $_POST['hulk_woo_ap_user_roles'] ) )
			);
		} else {
			delete_post_meta( $post_id, 'hulk_woo_ap_user_roles' );
		}
	}
}
