<?php

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

/**
 * Permission callback for dashboard REST API routes
 * 
 * @return boolean Whether the user can access the endpoint
 */
function content_craft_ai_dashboard_permission_callback()
{
  // Only allow authenticated users with API key
  $api_key = get_option('content_craft_ai_api_key');
  return !empty($api_key);
}

/**
 * Register dashboard routes
 */
function content_craft_ai_register_dashboard_routes()
{
  // Get subscription data
  register_rest_route('content-craft-ai/v1', '/dashboard/subscription', array(
    'methods' => 'GET',
    'callback' => 'content_craft_ai_get_subscription',
    'permission_callback' => 'content_craft_ai_dashboard_permission_callback',
  ));

  // Upgrade/change plan
  register_rest_route('content-craft-ai/v1', '/dashboard/upgrade-plan', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_upgrade_plan',
    'permission_callback' => 'content_craft_ai_dashboard_permission_callback',
    'args' => array(
      'planType' => array(
        'required' => true,
        'sanitize_callback' => 'sanitize_text_field',
      ),
    ),
  ));

  // Cancel subscription
  register_rest_route('content-craft-ai/v1', '/dashboard/cancel-subscription', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_cancel_subscription',
    'permission_callback' => 'content_craft_ai_dashboard_permission_callback',
  ));

  // Change plan
  register_rest_route('content-craft-ai/v1', '/dashboard/change-plan', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_change_plan',
    'permission_callback' => 'content_craft_ai_dashboard_permission_callback',
    'args' => array(
      'planType' => array(
        'required' => true,
        'sanitize_callback' => 'sanitize_text_field',
      ),
    ),
  ));

  // Get billing information
  register_rest_route('content-craft-ai/v1', '/dashboard/billing', array(
    'methods' => 'GET',
    'callback' => 'content_craft_ai_get_billing',
    'permission_callback' => 'content_craft_ai_dashboard_permission_callback',
  ));

  // Update billing information
  register_rest_route('content-craft-ai/v1', '/dashboard/billing', array(
    'methods' => 'POST',
    'callback' => 'content_craft_ai_update_billing',
    'permission_callback' => 'content_craft_ai_dashboard_permission_callback',
    'args' => array(
      'street' => array(
        'required' => true,
        'sanitize_callback' => 'sanitize_text_field',
      ),
      'city' => array(
        'required' => true,
        'sanitize_callback' => 'sanitize_text_field',
      ),
      'state' => array(
        'required' => false,
        'sanitize_callback' => 'sanitize_text_field',
      ),
      'country' => array(
        'required' => true,
        'sanitize_callback' => 'sanitize_text_field',
      ),
      'zipcode' => array(
        'required' => true,
        'sanitize_callback' => 'sanitize_text_field',
      ),
    ),
  ));
}

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