<?php
/**
 * Event Registry - Single Source of Truth for Notification Events
 * 
 * Defines all notification events in the system with their metadata.
 * Other components query this registry instead of hardcoding event lists.
 * 
 * @package WooNooW\Core\Notifications
 */

namespace WooNooW\Core\Notifications;

class EventRegistry {
	
	/**
	 * Get all registered notification events
	 * 
	 * This is the SINGLE SOURCE OF TRUTH for all events in the system.
	 * All other components (API, TemplateProvider, etc.) must use this.
	 * 
	 * @return array Event definitions with structure:
	 *   [
	 *     'event_id' => [
	 *       'id' => 'event_id',
	 *       'label' => 'Human readable label',
	 *       'description' => 'What triggers this event',
	 *       'category' => 'orders|products|customers',
	 *       'recipient_type' => 'staff|customer',
	 *       'wc_email' => 'woocommerce_email_id',
	 *       'enabled' => true|false,
	 *     ]
	 *   ]
	 */
	public static function get_all_events() {
    $events = [
        // ===== CUSTOMER ACCOUNT EVENTS =====
        'new_customer' => [
            'id' => 'new_customer',
            'label' => __('New Customer', 'woonoow'),
            'description' => __('When a new customer registers', 'woonoow'),
            'category' => 'customers',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_new_account',
            'enabled' => true,
        ],
        
        // ===== NEWSLETTER EVENTS =====
        'newsletter_welcome' => [
            'id' => 'newsletter_welcome',
            'label' => __('Newsletter Welcome', 'woonoow'),
            'description' => __('Welcome email sent when someone subscribes to newsletter', 'woonoow'),
            'category' => 'marketing',
            'recipient_type' => 'customer',
            'wc_email' => '',
            'enabled' => true,
        ],
        'newsletter_subscribed_admin' => [
            'id' => 'newsletter_subscribed_admin',
            'label' => __('New Newsletter Subscriber', 'woonoow'),
            'description' => __('Admin notification when someone subscribes to newsletter', 'woonoow'),
            'category' => 'marketing',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'newsletter_campaign' => [
            'id' => 'newsletter_campaign',
            'label' => __('Newsletter Campaign', 'woonoow'),
            'description' => __('Master email design template for newsletter campaigns', 'woonoow'),
            'category' => 'marketing',
            'recipient_type' => 'customer',
            'wc_email' => '',
            'enabled' => true,
            'variables' => [
                '{content}' => __('Campaign content', 'woonoow'),
                '{campaign_title}' => __('Campaign title', 'woonoow'),
                '{subscriber_email}' => __('Subscriber email', 'woonoow'),
                '{unsubscribe_url}' => __('Unsubscribe link', 'woonoow'),
            ],
        ],

        // ===== ORDER INITIATION =====
        'order_placed' => [
            'id' => 'order_placed',
            'label' => __('Order Placed', 'woonoow'),
            'description' => __('When a new order is placed', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => 'new_order',
            'enabled' => true,
        ],
        'order_placed_customer' => [
            'id' => 'order_placed',
            'label' => __('Order Placed', 'woonoow'),
            'description' => __('When customer places an order', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_on_hold_order',
            'enabled' => true,
        ],
        'order_pending' => [
            'id' => 'order_pending',
            'label' => __('Order Pending', 'woonoow'),
            'description' => __('When order is created (pending payment)', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'order_pending_customer' => [
            'id' => 'order_pending',
            'label' => __('Order Pending', 'woonoow'),
            'description' => __('When order is created (pending payment)', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => '',
            'enabled' => true,
        ],

        // ===== PAYMENT PROCESSING =====
        'order_on_hold' => [
            'id' => 'order_on_hold',
            'label' => __('Order On-Hold', 'woonoow'),
            'description' => __('When order is awaiting payment', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'order_on_hold_customer' => [
            'id' => 'order_on_hold',
            'label' => __('Order On-Hold', 'woonoow'),
            'description' => __('When order is awaiting payment', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_on_hold_order',
            'enabled' => true,
        ],
        'payment_received' => [
            'id' => 'payment_received',
            'label' => __('Payment Received', 'woonoow'),
            'description' => __('When payment is successfully received', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'payment_received_customer' => [
            'id' => 'payment_received',
            'label' => __('Payment Received', 'woonoow'),
            'description' => __('When payment is confirmed', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => '',
            'enabled' => true,
        ],
        'payment_failed' => [
            'id' => 'payment_failed',
            'label' => __('Payment Failed', 'woonoow'),
            'description' => __('When payment processing fails', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'payment_failed_customer' => [
            'id' => 'payment_failed',
            'label' => __('Payment Failed', 'woonoow'),
            'description' => __('When payment fails - prompt retry', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_failed_order',
            'enabled' => true,
        ],

        // ===== ORDER PROCESSING =====
        'order_processing' => [
            'id' => 'order_processing',
            'label' => __('Order Processing', 'woonoow'),
            'description' => __('When order is confirmed and being processed', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => 'customer_processing_order',
            'enabled' => true,
        ],
        'order_processing_customer' => [
            'id' => 'order_processing',
            'label' => __('Order Processing', 'woonoow'),
            'description' => __('When order status changes to processing', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_processing_order',
            'enabled' => true,
        ],

        // ===== FULFILLMENT =====
        'order_shipped' => [
            'id' => 'order_shipped',
            'label' => __('Order Shipped', 'woonoow'),
            'description' => __('When order is shipped', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'order_shipped_customer' => [
            'id' => 'order_shipped',
            'label' => __('Order Shipped', 'woonoow'),
            'description' => __('When order is shipped with tracking', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => '',
            'enabled' => true,
        ],
        'order_completed' => [
            'id' => 'order_completed',
            'label' => __('Order Completed', 'woonoow'),
            'description' => __('When order is marked as completed', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => 'customer_completed_order',
            'enabled' => true,
        ],
        'order_completed_customer' => [
            'id' => 'order_completed',
            'label' => __('Order Completed', 'woonoow'),
            'description' => __('When order is delivered/completed', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_completed_order',
            'enabled' => true,
        ],

        // ===== ORDER ISSUES / EXCEPTIONS =====
        'order_failed' => [
            'id' => 'order_failed',
            'label' => __('Order Failed', 'woonoow'),
            'description' => __('When order fails', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => 'failed_order',
            'enabled' => true,
        ],
        'order_failed_customer' => [
            'id' => 'order_failed',
            'label' => __('Order Failed', 'woonoow'),
            'description' => __('When order fails', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_failed_order',
            'enabled' => true,
        ],
        'order_cancelled' => [
            'id' => 'order_cancelled',
            'label' => __('Order Cancelled', 'woonoow'),
            'description' => __('When order is cancelled', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => 'cancelled_order',
            'enabled' => true,
        ],
        'order_cancelled_customer' => [
            'id' => 'order_cancelled',
            'label' => __('Order Cancelled', 'woonoow'),
            'description' => __('When order is cancelled', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_refunded_order',
            'enabled' => true,
        ],
        'order_refunded' => [
            'id' => 'order_refunded',
            'label' => __('Order Refunded', 'woonoow'),
            'description' => __('When order is refunded', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'staff',
            'wc_email' => '',
            'enabled' => true,
        ],
        'order_refunded_customer' => [
            'id' => 'order_refunded',
            'label' => __('Order Refunded', 'woonoow'),
            'description' => __('When order is refunded', 'woonoow'),
            'category' => 'orders',
            'recipient_type' => 'customer',
            'wc_email' => 'customer_refunded_order',
            'enabled' => true,
        ],
    ];
    
    /**
     * Filter: woonoow_notification_events_registry
     * 
     * Allows plugins/themes to add custom notification events
     * 
     * @param array $events Event definitions
     */
    return apply_filters('woonoow_notification_events_registry', $events);
}
	
	/**
	 * Get events by recipient type
	 * 
	 * @param string $recipient_type 'staff' or 'customer'
	 * @return array Filtered events
	 */
	public static function get_events_by_recipient($recipient_type) {
		$all_events = self::get_all_events();
		
		return array_filter($all_events, function($event) use ($recipient_type) {
			return $event['recipient_type'] === $recipient_type;
		});
	}
	
	/**
	 * Get events by category
	 * 
	 * @param string $category 'orders', 'products', 'customers'
	 * @return array Filtered events
	 */
	public static function get_events_by_category($category) {
		$all_events = self::get_all_events();
		
		return array_filter($all_events, function($event) use ($category) {
			return $event['category'] === $category;
		});
	}
	
	/**
	 * Get single event definition
	 * 
	 * @param string $event_id Event ID
	 * @param string $recipient_type Recipient type
	 * @return array|null Event definition or null if not found
	 */
	public static function get_event($event_id, $recipient_type) {
		$all_events = self::get_all_events();
		
		foreach ($all_events as $event) {
			if ($event['id'] === $event_id && $event['recipient_type'] === $recipient_type) {
				return $event;
			}
		}
		
		return null;
	}
	
	/**
	 * Check if event exists
	 * 
	 * @param string $event_id Event ID
	 * @param string $recipient_type Recipient type
	 * @return bool
	 */
	public static function event_exists($event_id, $recipient_type) {
		return self::get_event($event_id, $recipient_type) !== null;
	}
}
