<?php

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

/**
 * Get user notifications from the API
 * 
 * @param WP_REST_Request $request The request object
 * @return WP_REST_Response Response with notifications data
 */
function content_craft_ai_get_notifications($request = null)
{
    // Get pagination parameters if they exist
    $page = 1;
    $limit = 10;

    if ($request) {
        $page = $request->get_param('page') ? intval($request->get_param('page')) : 1;
        $limit = $request->get_param('limit') ? intval($request->get_param('limit')) : 10;
    }

    // Check if API key exists
    $api_key = get_option('content_craft_ai_api_key');
    if (empty($api_key)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Authentication required',
            ),
            401
        );
    }

    // Check if JWT token exists and is valid
    $token = get_option('content_craft_ai_token');
    if (empty($token)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'JWT authentication required',
            ),
            401
        );
    }

    // Make request to API
    $response = wp_remote_get(
        CONTENT_CRAFT_AI_EXTERNAL_API . '/notifications?page=' . $page . '&limit=' . $limit,
        array(
            'headers' => array(
                'x-api-key' => $api_key,
                'x-wp-domain' => wp_parse_url(get_site_url(), PHP_URL_HOST),
                'Content-Type' => 'application/json',
            ),
            'timeout' => 120,
        )
    );

    // Check for error
    if (is_wp_error($response)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => $response->get_error_message(),
            ),
            500
        );
    }

    $body = wp_remote_retrieve_body($response);
    $status = wp_remote_retrieve_response_code($response);

    // Handle API errors
    if ($status !== 200) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Error fetching notifications data',
                'data' => json_decode($body, true),
            ),
            $status
        );
    }

    // Return success response
    return new WP_REST_Response(
        json_decode($body, true),
        200
    );
}

/**
 * Dismiss a notification
 * 
 * @param WP_REST_Request $request The request object
 * @return WP_REST_Response Response with result
 */
function content_craft_ai_dismiss_notification($request)
{
    // Check if API key exists
    $api_key = get_option('content_craft_ai_api_key');
    if (empty($api_key)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Authentication required',
            ),
            401
        );
    }

    // Check if JWT token exists and is valid
    $token = get_option('content_craft_ai_token');
    if (empty($token)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'JWT authentication required',
            ),
            401
        );
    }

    // Get notification ID
    $notification_id = $request->get_param('id');
    if (empty($notification_id)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Notification ID is required',
            ),
            400
        );
    }

    // Make request to API
    $response = wp_remote_post(
        CONTENT_CRAFT_AI_EXTERNAL_API . '/notifications/' . $notification_id . '/dismiss',
        array(
            'method' => 'POST',
            'headers' => array(
                'x-api-key' => $api_key,
                'x-wp-domain' => wp_parse_url(get_site_url(), PHP_URL_HOST),
                'Content-Type' => 'application/json',
            ),
            'timeout' => 120,
        )
    );

    // Check for error
    if (is_wp_error($response)) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => $response->get_error_message(),
            ),
            500
        );
    }

    $body = wp_remote_retrieve_body($response);
    $status = wp_remote_retrieve_response_code($response);

    // Handle API errors
    if ($status !== 200) {
        return new WP_REST_Response(
            array(
                'success' => false,
                'message' => 'Error dismissing notification',
                'data' => json_decode($body, true),
            ),
            $status
        );
    }

    // Return success response
    return new WP_REST_Response(
        json_decode($body, true),
        200
    );
}
