<?php
/**
 * Pricing Endpoint (Week 26)
 *
 * Proxies backend pricing catalog to the WP admin UI.
 *
 * @package BoostBot
 */

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

class BoostBot_Pricing_Endpoint extends BoostBot_Base_Endpoint {

    /**
     * @var string
     */
    protected $rest_base = 'pricing';

    public function register() {
        // GET /wp-json/boostbot/v1/pricing/catalog
        $this->register_route(
            '/catalog',
            'GET',
            array( $this, 'get_catalog' ),
            array( $this, 'admin_access' )
        );
    }

    public function get_catalog( $request ) {
        $backend_base_url = trim( (string) BoostBot_Settings::get( 'backend_base_url', '' ) );
        if ( empty( $backend_base_url ) ) {
            return $this->error( __( 'Credits backend is not configured.', 'boostbot' ), 400 );
        }

        if ( ! class_exists( 'BoostBot_Credits_Backend_Client' ) ) {
            return $this->error( __( 'Credits backend client is not available.', 'boostbot' ), 500 );
        }

        $client = new BoostBot_Credits_Backend_Client();
        $catalog = $client->get_catalog();
        if ( is_wp_error( $catalog ) ) {
            return $this->error( $catalog->get_error_message(), 500 );
        }

        return $this->success( $catalog );
    }
}


