<?php

/**
 * Notifications Controller
 * 
 * Handles business logic for notifications
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Get cached notifications or fetch fresh ones
 * 
 * @param boolean $force_refresh Whether to force a refresh from the API
 * @return array Notifications data
 */
function content_craft_ai_get_cached_notifications($force_refresh = false)
{
    // Check for cached notifications
    $cached_notifications = get_transient('content_craft_ai_notifications');

    // Return cached notifications if available and not forcing refresh
    if ($cached_notifications !== false && !$force_refresh) {
        return $cached_notifications;
    }

    // No cache or forced refresh, fetch from API
    if (function_exists('content_craft_ai_is_authenticated') && content_craft_ai_is_authenticated()) {
        // Use WP_REST_Request to call our own REST API
        $request = new WP_REST_Request('GET', '/content-craft-ai/v1/notifications');
        $response = rest_do_request($request);

        if (!$response->is_error()) {
            $data = $response->get_data();

            if (isset($data['success']) && $data['success'] === true && isset($data['data'])) {
                // Cache the notifications for 5 minutes
                set_transient('content_craft_ai_notifications', $data, 5 * MINUTE_IN_SECONDS);
                return $data;
            }
        }
    }

    // Return empty notifications if fetch failed
    return array(
        'success' => true,
        'data' => array(
            'notifications' => array()
        )
    );
}

/**
 * Check if there are any high-priority notifications
 * 
 * @return boolean Whether high priority notifications exist
 */
function content_craft_ai_has_high_priority_notifications()
{
    $notifications_data = content_craft_ai_get_cached_notifications();

    if (
        isset($notifications_data['success']) &&
        $notifications_data['success'] === true &&
        isset($notifications_data['data']['notifications'])
    ) {

        $notifications = $notifications_data['data']['notifications'];

        foreach ($notifications as $notification) {
            if (isset($notification['priority']) && $notification['priority'] === 'high') {
                return true;
            }
        }
    }

    return false;
}

/**
 * Count unread notifications
 * 
 * @return int Number of unread notifications
 */
function content_craft_ai_count_notifications()
{
    $notifications_data = content_craft_ai_get_cached_notifications();

    if (
        isset($notifications_data['success']) &&
        $notifications_data['success'] === true &&
        isset($notifications_data['data']['notifications'])
    ) {

        return count($notifications_data['data']['notifications']);
    }

    return 0;
}

/**
 * Enqueue notification assets and add data to page
 */
function content_craft_ai_enqueue_notification_assets()
{
    // Only load on plugin pages
    $screen = get_current_screen();
    if (strpos($screen->id, 'content-craft-ai') === false) {
        return;
    }

    // Get notifications count
    $notifications_count = content_craft_ai_count_notifications();
    $has_high_priority = content_craft_ai_has_high_priority_notifications();

    // Add notifications data to page
    wp_localize_script(
        'content-craft-ai-admin',
        'contentCraftAINotifications',
        array(
            'count' => $notifications_count,
            'hasHighPriority' => $has_high_priority
        )
    );
}

// Add notification assets to admin pages
add_action('admin_enqueue_scripts', 'content_craft_ai_enqueue_notification_assets');
