<?php
/**
 * Subscriptions Endpoint (Week 26)
 *
 * Backend-owned PayPal subscriptions. Browser calls WP REST, WP proxies to backend.
 *
 * @package BoostBot
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class BoostBot_Subscriptions_Endpoint extends BoostBot_Base_Endpoint {
    /**
     * @var string
     */
    protected $rest_base = 'subscriptions';

    public function register() {
        // POST /wp-json/boostbot/v1/subscriptions/create
        $this->register_route(
            '/create',
            'POST',
            array( $this, 'create_subscription' ),
            array( $this, 'admin_access' ),
            array(
                'package_id' => array(
                    'required' => true,
                    'type'     => 'string',
                ),
            )
        );

        // GET /wp-json/boostbot/v1/subscriptions/status
        $this->register_route(
            '/status',
            'GET',
            array( $this, 'get_status' ),
            array( $this, 'admin_access' )
        );
    }

    public function create_subscription( $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 );
        }

        $package_id = sanitize_text_field( (string) $request->get_param( 'package_id' ) );
        if ( empty( $package_id ) ) {
            return $this->error( __( 'package_id is required.', 'boostbot' ), 400 );
        }

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

        // UX: After PayPal approval, land directly on the Usage page.
        // Keep query params BEFORE the hash so PayPal can append params, and the admin SPA opens on `#/usage`.
        $return_url = admin_url( 'admin.php?page=boostbot&bb_subscription=success' ) . '#/usage';
        $cancel_url = admin_url( 'admin.php?page=boostbot&bb_subscription=cancel' ) . '#/usage';

        $resp = $client->create_paypal_subscription( (string) $site_id, $package_id, $return_url, $cancel_url );
        if ( is_wp_error( $resp ) ) {
            return $this->error( $resp->get_error_message(), 500 );
        }

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

    public function get_status( $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();
        $site_id = $client->ensure_site_id();
        if ( is_wp_error( $site_id ) ) {
            return $this->error( $site_id->get_error_message(), 500 );
        }

        $resp = $client->get_subscription_status( (string) $site_id );
        if ( is_wp_error( $resp ) ) {
            return $this->error( $resp->get_error_message(), 500 );
        }

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

