<?php
// components/Data/Notifications.php
namespace Components\Data;

use Components\Base;

class Notifications extends Base
{
    private static $instance = null;

    private static function getLegacy()
    {
        if (self::$instance === null) {
                $path = MH_DIR_FS_CATALOG . MH_ROOT_PATH . 'configbeez/config_customer_insight/classes/notificationbeez.php';
                if ($path && file_exists($path)) {
                    require_once($path);
                } else {
                    dd('notificationbeez not found');
                }

            
            if (class_exists('notificationbeez')) {
                self::$instance = new \notificationbeez();
            }
        }
        return self::$instance;
    }

    /**
     * Add a new notification
     * 
     * @param array $params [
     *   'title' => string,
     *   'summary' => string,
     *   'message' => string,
     *   'ref_id' => string|int,
     *   'module' => string,
     *   'severity' => int (1-Alert, 3-Warning, 5-Note),
     *   'link' => string,
     *   'icon' => string (SVG identifier),
     *   'click_action' => string
     * ]
     * @return bool
     */
    public static function add(array $params)
    {
        $legacy = self::getLegacy();
        if (!$legacy) return false;

        // Map modern params to legacy params
        $legacyParams = [
            'title' => $params['title'] ?? '',
            'notification_summary' => $params['summary'] ?? $params['title'] ?? '',
            'notification' => $params['message'] ?? '',
            'ref_id' => $params['ref_id'] ?? '',
            'iteration' => $params['iteration'] ?? 1,
            'module' => $params['module'] ?? 'beezui',
            'customers_id' => $params['customers_id'] ?? 0,
            'channel_id' => $params['channel_id'] ?? 0,
            'severity' => $params['severity'] ?? 5, 
            'link' => $params['link'] ?? '',
            'link_type' => $params['link_type'] ?? 'I', // I - internal (Router), E - external
            'icon_style' => $params['icon'] ?? 'bell', 
            'status' => 0,
            'panel_click_action' => $params['click_action'] ?? '',
        ];

        return $legacy->add($legacyParams);
    }

    /**
     * Get summary of open notifications for polling
     * 
     * @return array
     */
    public static function getPollSummary()
    {
        $legacy = self::getLegacy();
        if (!$legacy) return ['count' => 0, 'alert_count' => 0, 'low_severity_count' => 0];
        
        $open = self::getList(0, 100);
        $count = count($open);
        $alert_count = 0;
        $low_severity_count = 0;
        foreach ($open as $n) {
            // Severity 1 and 2 are considered alerts
            if ((int)$n['severity'] <= 2) {
                $alert_count++;
            } else {
                $low_severity_count++;
            }
        }
        
        return [
            'count' => $count,
            'alert_count' => $alert_count,
            'low_severity_count' => $low_severity_count,
        ];
    }

    /**
     * Get count of open notifications
     */
    public static function getOpenCount()
    {
        $legacy = self::getLegacy();
        return $legacy ? $legacy->getNotificationCount(0) : 0;
    }

    /**
     * Get list of notifications
     */
    public static function getList($status = 0, $limit = 50)
    {
        $legacy = self::getLegacy();
        if (!$legacy) return [];
        return $legacy->getNotificationList($limit, null, null, null, null, $status);
    }

    /**
     * Dismiss a notification
     */
    public static function dismiss($id)
    {
        $legacy = self::getLegacy();
        return $legacy ? $legacy->updateStatus($id, 1) : false;
    }

    /**
     * Dismiss all open notifications
     */
    public static function dismissAll()
    {
        $legacy = self::getLegacy();
        if (!$legacy) return false;
        
        $open = self::getList(0, 500);
        foreach ($open as $n) {
            $legacy->updateStatus($n['id'], 1);
        }
        return true;
    }
}
