<?php

/**
 * Channel Registry
 * 
 * Manages registration and retrieval of notification channels
 * 
 * @package WooNooW\Core\Notifications
 */

namespace WooNooW\Core\Notifications;

use WooNooW\Core\Notifications\Channels\ChannelInterface;

class ChannelRegistry
{

    /**
     * Registered channels
     * 
     * @var array<string, ChannelInterface>
     */
    private static $channels = [];

    /**
     * Register a notification channel
     * 
     * @param ChannelInterface $channel Channel instance
     * @return bool Success status
     */
    public static function register(ChannelInterface $channel)
    {
        $id = $channel->get_id();

        if (empty($id)) {
            return false;
        }

        self::$channels[$id] = $channel;
        return true;
    }

    /**
     * Get a registered channel by ID
     * 
     * @param string $channel_id Channel identifier
     * @return ChannelInterface|null Channel instance or null if not found
     */
    public static function get($channel_id)
    {
        return self::$channels[$channel_id] ?? null;
    }

    /**
     * Get all registered channels
     * 
     * @return array<string, ChannelInterface> Associative array of channel_id => channel_instance
     */
    public static function get_all()
    {
        return self::$channels;
    }

    /**
     * Check if a channel is registered
     * 
     * @param string $channel_id Channel identifier
     * @return bool True if channel exists
     */
    public static function has($channel_id)
    {
        return isset(self::$channels[$channel_id]);
    }

    /**
     * Unregister a channel
     * 
     * @param string $channel_id Channel identifier
     * @return bool Success status
     */
    public static function unregister($channel_id)
    {
        if (isset(self::$channels[$channel_id])) {
            unset(self::$channels[$channel_id]);
            return true;
        }
        return false;
    }

    /**
     * Get list of configured channel IDs
     * 
     * Only returns channels that are properly configured (is_configured() returns true)
     * 
     * @return array List of configured channel IDs
     */
    public static function get_configured_channels()
    {
        $configured = [];
        foreach (self::$channels as $id => $channel) {
            if ($channel->is_configured()) {
                $configured[] = $id;
            }
        }
        return $configured;
    }
}
