<?php

namespace CouponsPlus\App\Handlers;

use CouponsPlus\Original\Events\Handler\EventHandler;
use CouponsPlus\App\Data\NoticesData;

#[\AllowDynamicProperties]
Class BlocksNoticeHandler extends EventHandler
{
    protected $numberOfArguments = 1;
    protected $priority = 10;

    public function execute()
    {
        // Check if the Store API function exists
        if (!function_exists('woocommerce_store_api_register_endpoint_data')) {
            return;
        }

        // Register notice extension for cart endpoint
        woocommerce_store_api_register_endpoint_data(
            array(
                'endpoint'        => 'cart',
                'namespace'       => 'coupons-plus',
                'data_callback'   => array($this, 'extend_cart_data'),
                'schema_callback' => array($this, 'extend_cart_schema'),
                'schema_type'     => ARRAY_A,
            )
        );

        // Register notice extension for checkout endpoint
        woocommerce_store_api_register_endpoint_data(
            array(
                'endpoint'        => 'checkout',
                'namespace'       => 'coupons-plus',
                'data_callback'   => array($this, 'extend_checkout_data'),
                'schema_callback' => array($this, 'extend_checkout_schema'),
                'schema_type'     => ARRAY_A,
            )
        );
    }

    /**
     * Add custom notice data to cart endpoint
     *
     * @return array Custom notice data
     */
    public function extend_cart_data()
    {
        return $this->get_notice_data();
    }

    /**
     * Add custom notice data to checkout endpoint
     *
     * @return array Custom notice data
     */
    public function extend_checkout_data()
    {
        return $this->get_notice_data();
    }

    /**
     * Get the notice data to display
     *
     * @return array Notice data
     */
    protected function get_notice_data()
    {
        return array(
            'notices' => NoticesData::getNotices(),
        );
    }

    /**
     * Schema for cart custom data
     *
     * @return array Schema definition
     */
    public function extend_cart_schema()
    {
        return $this->get_notice_schema();
    }

    /**
     * Schema for checkout custom data
     *
     * @return array Schema definition
     */
    public function extend_checkout_schema()
    {
        return $this->get_notice_schema();
    }

    /**
     * Get the schema definition for notices
     *
     * @return array Schema definition
     */
    protected function get_notice_schema()
    {
        return array(
            'notices' => array(
                'description' => 'Array of custom notices from Coupons Plus',
                'type'        => 'array',
                'readonly'    => true,
                'items'       => array(
                    'type'       => 'object',
                    'properties' => array(
                        'message' => array(
                            'description' => 'The notice message',
                            'type'        => 'string',
                            'readonly'    => true,
                        ),
                        'type' => array(
                            'description' => 'The notice type (info, success, warning, error)',
                            'type'        => 'string',
                            'readonly'    => true,
                        ),
                        'button' => array(
                            'description' => 'Button configuration',
                            'type'        => 'object',
                            'readonly'    => true,
                            'properties'  => array(
                                'text' => array(
                                    'description' => 'Button text',
                                    'type'        => 'string',
                                    'readonly'    => true,
                                ),
                                'url' => array(
                                    'description' => 'Button URL',
                                    'type'        => 'string',
                                    'readonly'    => true,
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        );
    }
}
