<?php
/**
 * Usage Endpoint for BoostBot REST API.
 *
 * @package BoostBot
 */

// Prevent direct access to the file.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * BoostBot_Usage_Endpoint class.
 *
 * Handles usage statistics and analytics.
 *
 * @since 1.0.0
 */
class BoostBot_Usage_Endpoint extends BoostBot_Base_Endpoint {

    /**
     * The base path for this endpoint.
     *
     * @since 1.0.0
     * @access protected
     * @var string $rest_base
     */
    protected $rest_base = 'usage';

    /**
     * Register the routes for this endpoint.
     *
     * @since 1.0.0
     */
    public function register() {
        // GET /wp-json/boostbot/v1/usage/conversations - Get conversations count (across all bots)
        $this->register_route(
            '/conversations',
            'GET',
            array( $this, 'get_conversations' ),
            array( $this, 'admin_access' )
        );
    }

    /**
     * Get conversations count summary.
     *
     * @since 1.0.0
     * @param WP_REST_Request $request The REST request object.
     * @return WP_REST_Response The response.
     */
    public function get_conversations( $request ) {
        $days = (int) $request->get_param( 'days' ) ?: 30;
        $conversation_model = new BoostBot_Conversation_Model();
        $stats = $conversation_model->get_statistics( $days );

        return $this->success( array(
            'summary' => array(
                'total_conversations' => (int) ( $stats['total_conversations'] ?? 0 ),
            ),
        ) );
    }
}

