<?php

namespace CouponsPlus\App\Handlers;

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

#[\AllowDynamicProperties]
Class ProductPageNoticeHandler extends EventHandler
{
    protected $priority = 20;

    public function execute()
    {
        // Show on product pages and classic cart pages
        // Skip on checkout to avoid conflicts
        if (is_checkout()) {
            return;
        }

        // Get notices from NoticesData
        $notices = NoticesData::getNotices();
        
        if (empty($notices)) {
            return;
        }
        
        // Add each notice to WooCommerce's notice system
        foreach ($notices as $notice) {
            $this->addNotice($notice);
        }
    }

    /**
     * Add a single notice to WooCommerce
     *
     * @param array $notice Notice data
     */
    protected function addNotice($notice)
    {
        // Check if this notice ID already exists
        if (isset($notice['id']) && $this->noticeExists($notice['id'])) {
            return;
        }
        
        // Map our types to WooCommerce types
        $wcType = $this->mapNoticeType($notice['type']);
        
        // Build the message HTML with custom class wrapper
        $message = '<span class="coupons-plus-classic-notice">';
        $message .= esc_html($notice['message']);
        
        // Add button if provided
        if (isset($notice['button']) && !empty($notice['button']['text']) && !empty($notice['button']['url'])) {
            $message .= sprintf(
                ' <a href="%s" class="button wc-forward">%s</a>',
                esc_url($notice['button']['url']),
                esc_html($notice['button']['text'])
            );
        }
        
        $message .= '</span>';
        
        // Add to WooCommerce notices with ID in data
        $data = isset($notice['id']) ? array('coupons_plus_id' => $notice['id']) : array();
        wc_add_notice($message, $wcType, $data);
    }

    /**
     * Check if a notice with this ID already exists
     *
     * @param string $notice_id Notice ID to check
     * @return bool True if notice exists
     */
    protected function noticeExists($notice_id)
    {
        $all_notices = wc_get_notices();
        
        foreach ($all_notices as $type => $notices) {
            foreach ($notices as $notice) {
                if (isset($notice['data']['coupons_plus_id']) && $notice['data']['coupons_plus_id'] === $notice_id) {
                    return true;
                }
            }
        }
        
        return false;
    }

    /**
     * Map our notice types to WooCommerce notice types
     *
     * @param string $type Our notice type
     * @return string WooCommerce notice type
     */
    protected function mapNoticeType($type)
    {
        $typeMap = array(
            'info'    => 'notice',
            'success' => 'success',
            'warning' => 'notice',
            'error'   => 'error',
        );

        return isset($typeMap[$type]) ? $typeMap[$type] : 'notice';
    }
}
