<?php

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

/**
 * Get WordPress categories for article generator
 * 
 * @param WP_REST_Request $request The request object
 * @return WP_REST_Response The response object
 */
function content_craft_ai_handle_get_categories($request)
{
    try {
        // Get categories from WordPress
        $categories = get_categories([
            'hide_empty' => false,
            'orderby' => 'name',
            'order' => 'ASC'
        ]);

        // Format categories for API response
        $formatted_categories = [];
        foreach ($categories as $category) {
            $formatted_categories[] = [
                'id' => $category->term_id,
                'name' => $category->name,
                'slug' => $category->slug,
                'count' => $category->count
            ];
        }

        // Return success response
        return new WP_REST_Response([
            'success' => true,
            'data' => $formatted_categories
        ], 200);
    } catch (Exception $e) {
        return new WP_REST_Response([
            'success' => false,
            'message' => $e->getMessage()
        ], 500);
    }
}
