<?php

/**
 * Domain Verification API Functions
 */

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

/**
 * Get verification token
 * Allows external service to verify domain ownership
 * 
 * @return WP_REST_Response API response with verification token
 */
function content_craft_ai_get_verification()
{
  $token = ContentCraftAI_VerificationManager::get_token();
  $domain = ContentCraftAI_VerificationManager::get_site_domain();

  if (!$token) {
    return rest_ensure_response([
      'success' => false,
      'message' => 'No verification token found',
    ]);
  }

  return rest_ensure_response([
    'success' => true,
    'domain' => $domain,
    'verification_token' => $token,
  ]);
}

/**
 * Set verification token
 * Public endpoint for initial domain verification
 * 
 * @param WP_REST_Request $request Request object
 * @return WP_REST_Response API response
 */
function content_craft_ai_set_verification($request)
{
  // Get parameters
  $token = $request->get_param('token');

  // Validate token
  if (empty($token)) {
    return rest_ensure_response([
      'success' => false,
      'message' => 'Token parameter is required',
    ]);
  }

  // Store the token
  $result = ContentCraftAI_VerificationManager::store_token($token);

  return rest_ensure_response([
    'success' => $result,
    'message' => $result ? 'Verification token saved' : 'Failed to save verification token',
  ]);
}

/**
 * Delete verification token
 * Public endpoint to remove verification token
 * 
 * @param WP_REST_Request $request Request object
 * @return WP_REST_Response API response
 */
function content_craft_ai_delete_verification($request)
{
  // Delete the token
  $result = ContentCraftAI_VerificationManager::delete_token();

  return rest_ensure_response([
    'success' => $result,
    'message' => $result ? 'Verification token deleted' : 'Failed to delete verification token',
  ]);
}
