<?php

namespace Storeplugin\PDFInvoice\App;

defined('ABSPATH') || exit;

use Storeplugin\PDFInvoice\Framework\Contracts\ServiceInterface;
use Storeplugin\PDFInvoice\Framework\Utils\Utils as FrameworkUtils;
use Storeplugin\PDFInvoice\App\Settings\SettingsSchema;

class Utils implements ServiceInterface {
    public const GROUP_INVOICE_EDITOR = SettingsSchema::GROUP_INVOICE_EDITOR;
    public const GROUP_PACKING_SLIP_EDITOR = SettingsSchema::GROUP_PACKING_SLIP_EDITOR;
    public const GROUP_SETTINGS = SettingsSchema::GROUP_SETTINGS;

    public function register(): void {
        add_filter( 'plugin_action_links_' . NXTPDF_BASE, [ Utils::class, 'setting_link'] );
        add_action( 'admin_menu', [ $this, 'registerAdminMenu' ], 9 );
    }

    /**
	 * Setting page link from plugin page.
	 *
	 * @param array $links
	 * @return array
	 */
	public static function setting_link( $links ) {
		$links[] = '<a href="' . admin_url( 'admin.php?page=nxtpdf-invoice-editor-panel' ) . '">Settings</a>';
		return $links;
	}

    /**
     * Add main admin menu for order invoice editor panel
     *
     * @return void
     */
    public function registerAdminMenu(): void
    {
        \add_menu_page(
            'Invoice Editor',
            'Store Invoice',
            'manage_options',
            'nxtpdf-invoice-editor-panel',
            null,
            'dashicons-media-document',
        );
    }

    /**
     * Get an option from a specific option group.
     *
     * @param string $key Option key inside the group payload.
     * @param string $group Option group key.
     * @param mixed  $default Default value.
     * @return mixed
     */
    public static function getOption(string $key, string $group, $default = false)
    {
        $key = sanitize_key($key);
        if ($key === '') {
            return $default;
        }

        $options = self::getOptions($group);

        return array_key_exists($key, $options) ? $options[$key] : $default;
    }

    /**
     * Update a single option in a specific option group.
     *
     * @param string $key Option key inside the group payload.
     * @param mixed  $value Option value.
     * @param string $group Option group key.
     * @return bool
     */
    public static function updateOption(string $key, $value, string $group): bool
    {
        $key = sanitize_key($key);
        if ($key === '') {
            return false;
        }

        $options = self::getOptions($group);
        $options[$key] = $value;

        return self::updateOptions($group, $options);
    }

    /**
     * Get the full options payload for a group.
     *
     * @param string $group Option group key.
     * @return array<string, mixed>
     */
    public static function getOptions(string $group): array
    {
        $group = sanitize_key($group);
        if ($group === '') {
            return [];
        }

        $options = FrameworkUtils::getOptions($group);

        return SettingsSchema::normalizeOptionsForGroup($options, $group);
    }

    /**
     * Replace and persist a full options payload for a group.
     *
     * @param string $group Option group key.
     * @param array  $options Option payload.
     * @return bool
     */
    public static function updateOptions(string $group, array $options): bool
    {
        $group = sanitize_key($group);
        if ($group === '') {
            return false;
        }

        $options = SettingsSchema::normalizeOptionsForGroup($options, $group);

        return (bool) update_option($group, $options);
    }
}
