<?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 function HulkPlugins\Woo\AccountPages\hulk_woo_ap_fs;
use function HulkPlugins\Woo\AccountPages\hulk_woo_ap_fs_notice;
use const HulkPlugins\Woo\AccountPages\PLUGIN_DIR_PATH;
use const HulkPlugins\Woo\AccountPages\PLUGIN_DIR_URL;

defined( 'ABSPATH' ) || exit;

class AccountPageMetaBoxController extends BaseController {

	use SingletonTrait;

	protected AccountPagesService $pages_service;

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

        add_action( 'init', [ $this, 'register_gutenberg_compatible_meta' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'block_editor_assets' ] );
	}

	/**
	 * Register gutenberg block editor compatible meta
	 */
	public function register_gutenberg_compatible_meta(): void {
		register_post_meta(
			$this->pages_service->post_type,
            'hulk_woo_ap_user_roles',
            [
				'type'              => 'array',
				'single'            => true,
				'default'           => [],
				'show_in_rest'      => [
					'schema' => [
						'type'  => 'array',
						'items' => [
							'type' => 'string',
						],
					],
				],
				'sanitize_callback' => function ( $value ) {
					return array_map( 'sanitize_text_field', (array) $value );
				},
				'auth_callback'     => function () {
					return current_user_can( 'edit_posts' );
				},
            ]
        );
	}

	/**
	 * Enqueue block editor assets
	 */
	public function block_editor_assets(): void {
		$screen = get_current_screen();

		// Exit early if the post type does not match
		if ( ! $screen || ($screen->post_type !== $this->pages_service->post_type) ) {
			return;
		}

		$asset = require PLUGIN_DIR_PATH . 'build/admin/account/index.asset.php';

		wp_enqueue_style(
			'hulk_woo_ap_account_page_assets',
			PLUGIN_DIR_URL . 'build/admin/account/index.css',
			[ 'wp-components' ],
			$asset['version'],
		);

		wp_enqueue_script(
			'hulk_woo_ap_account_page_assets',
			PLUGIN_DIR_URL . 'build/admin/account/index.js',
			$asset['dependencies'],
			$asset['version'],
			true
		);

		$metaService = AccountPageMetaBoxService::getInstance();

		$system_roles = $metaService->get_system_roles();

		$licensing = [
			'can_use_premium_code' => hulk_woo_ap_fs()->can_use_premium_code(),
			'is_premium'           => hulk_woo_ap_fs()->is_premium(),
			'checkout_url'         => hulk_woo_ap_fs()->checkout_url(),
			'is_not_paying'        => hulk_woo_ap_fs()->is_not_paying(),
			'get_upgrade_url'      => hulk_woo_ap_fs()->get_upgrade_url(),
			'notice'               => hulk_woo_ap_fs_notice(),
		];

		wp_localize_script(
			'hulk_woo_ap_account_page_assets',
			'hulk_woo_ap_account_object',
			[
				'PLUGIN_DIR_URL'   => PLUGIN_DIR_URL,
				'system_roles'     => $system_roles,
				'account_page_url' => $metaService->get_account_page_url(),
				'add_new_page_url' => admin_url( 'post-new.php?post_type=hulk_woo_ap_page' ),
				'licensing'        => $licensing,
			]
		);
	}
}
