<?php

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

/**
 * Permission callback for article generator REST API routes
 * 
 * @return boolean Whether the user can access the endpoint
 */
function content_craft_ai_article_generator_permission_callback()
{
    // Check if user is logged in and has proper capabilities
    if (!current_user_can('publish_posts')) {
        return false;
    }

    // Only allow authenticated users with a valid token
    $token = get_option('content_craft_ai_token');
    if (empty($token)) {
        return false;
    }

    // Verify nonce for CSRF protection
    $nonce_verified = wp_verify_nonce(
        isset($_SERVER['HTTP_X_WP_NONCE']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])) : '',
        'wp_rest'
    );

    if (!$nonce_verified) {
        // error_log('Content Craft AI: Article generator nonce verification failed');
        return false;
    }

    return true;
}

/**
 * Permission callback for administrative article generator operations
 * 
 * @return boolean Whether the user can access the admin endpoint
 */
function content_craft_ai_article_generator_admin_permission_callback()
{
    // Check if user is logged in and has administrator capabilities
    if (!current_user_can('manage_options')) {
        return false;
    }

    // Only allow authenticated users with a valid token
    $token = get_option('content_craft_ai_token');
    if (empty($token)) {
        return false;
    }

    // Verify nonce for CSRF protection
    $nonce_verified = wp_verify_nonce(
        isset($_SERVER['HTTP_X_WP_NONCE']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_NONCE'])) : '',
        'wp_rest'
    );

    if (!$nonce_verified) {
        // error_log('Content Craft AI: Admin operation nonce verification failed');
        return false;
    }

    return true;
}

/**
 * Register article generator routes
 */
function content_craft_ai_register_article_generator_routes()
{
    // Generate single article
    register_rest_route('content-craft-ai/v1', '/article-generator/generate-single', array(
        'methods' => 'POST',
        'callback' => 'content_craft_ai_handle_single_article_generation',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));

    // Generate bulk articles
    register_rest_route('content-craft-ai/v1', '/article-generator/generate-bulk', array(
        'methods' => 'POST',
        'callback' => 'content_craft_ai_handle_bulk_article_generation',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));

    // Get article generation status
    register_rest_route('content-craft-ai/v1', '/article-generator/status/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
        'methods' => 'GET',
        'callback' => 'content_craft_ai_handle_article_status',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));

    // Get article generation history
    register_rest_route('content-craft-ai/v1', '/article-generator/history', array(
        'methods' => 'GET',
        'callback' => 'content_craft_ai_handle_article_history',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));

    // Generate a scheduled article immediately
    register_rest_route('content-craft-ai/v1', '/article-generator/generate-scheduled/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
        'methods' => 'POST',
        'callback' => 'content_craft_ai_handle_generate_scheduled_article',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));

    // Reschedule an article
    register_rest_route('content-craft-ai/v1', '/article-generator/reschedule/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
        'methods' => 'POST',
        'callback' => 'content_craft_ai_handle_reschedule_article',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));

    // Force check for scheduled articles (admin operation)
    register_rest_route('content-craft-ai/v1', '/article-generator/check-scheduled', array(
        'methods' => 'POST',
        'callback' => 'content_craft_ai_handle_check_scheduled_articles',
        'permission_callback' => 'content_craft_ai_article_generator_admin_permission_callback',
    ));

    // Cancel scheduled article
    register_rest_route('content-craft-ai/v1', '/article-generator/cancel/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
        'methods' => 'POST',
        'callback' => 'content_craft_ai_handle_cancel_article',
        'permission_callback' => 'content_craft_ai_article_generator_permission_callback',
    ));
}

// Register routes
add_action('rest_api_init', 'content_craft_ai_register_article_generator_routes');
