<?php

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

/**
 * Permission callback for article status REST API routes
 * 
 * @return boolean Whether the user can access the endpoint
 */
function content_craft_ai_article_status_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 status nonce verification failed');
    return false;
  }

  return true;
}

/**
 * Register article status routes
 */
function content_craft_ai_register_article_status_routes()
{
  // Get article generations with filters
  register_rest_route('content-craft-ai/v1', '/article-status/generations', array(
    'methods' => 'GET',
    'callback' => 'content_craft_ai_handle_get_generations',
    'permission_callback' => 'content_craft_ai_article_status_permission_callback',
  ));

  // Get generation details for editing
  register_rest_route('content-craft-ai/v1', '/article-status/generations/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
    'methods' => 'GET',
    'callback' => 'content_craft_ai_handle_get_generation_details',
    'permission_callback' => 'content_craft_ai_article_status_permission_callback',
  ));

  // Cancel an article generation
  register_rest_route('content-craft-ai/v1', '/article-status/cancel/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_handle_cancel_generation',
    'permission_callback' => 'content_craft_ai_article_status_permission_callback',
  ));

  // Restart a failed or cancelled article generation
  register_rest_route('content-craft-ai/v1', '/article-status/restart/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_handle_restart_generation',
    'permission_callback' => 'content_craft_ai_article_status_permission_callback',
  ));

  // Update a scheduled article
  register_rest_route('content-craft-ai/v1', '/article-status/update/(?P<queue_id>[a-zA-Z0-9-_]+)', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_handle_update_scheduled_article',
    'permission_callback' => 'content_craft_ai_article_status_permission_callback',
  ));

  // Get all generations with filters
  register_rest_route('content-craft-ai/v1', '/article-status/get-generations', [
    'methods'  => 'POST',
    'callback' => 'content_craft_ai_get_generations',
    'permission_callback' => function () {
      return current_user_can('manage_options');
    },
  ]);

  // Cancel a generation
  register_rest_route('content-craft-ai/v1', '/article-status/cancel-generation', [
    'methods'  => 'POST',
    'callback' => 'content_craft_ai_cancel_generation',
    'permission_callback' => function () {
      return current_user_can('manage_options');
    },
  ]);

  // Edit a scheduled article
  register_rest_route('content-craft-ai/v1', '/article-status/edit-scheduled-article', [
    'methods'  => ['GET', 'POST'],
    'callback' => 'content_craft_ai_edit_scheduled_article',
    'permission_callback' => function () {
      return current_user_can('manage_options');
    },
  ]);

  // Restart a failed or cancelled generation
  register_rest_route('content-craft-ai/v1', '/article-status/restart-generation', [
    'methods'  => 'POST',
    'callback' => 'content_craft_ai_restart_generation',
    'permission_callback' => function () {
      return current_user_can('manage_options');
    },
  ]);
}

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