<?php
/**
 * Google API Endpoint
 *
 * Proxied Google Calendar integration management.
 * OAuth flow and API calls are handled by the centralized backend.
 *
 * @package BoostBot
 * @since   1.0.0
 * @updated 1.10.0 — Migrated to backend proxy
 */

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

/**
 * Class BoostBot_Google_Endpoint
 *
 * REST API endpoints for Google Calendar integration.
 */
class BoostBot_Google_Endpoint extends BoostBot_Base_Endpoint {

    /**
     * REST API namespace.
     *
     * @var string
     */
    protected $namespace = 'boostbot/v1';

    /**
     * REST API base.
     *
     * @var string
     */
    protected $rest_base = 'google';

    /**
     * Register routes.
     *
     * @return void
     */
    public function register(): void {
        // Get connection status
        $this->register_route(
            '/status',
            'GET',
            array( $this, 'get_status' ),
            array( $this, 'admin_access' )
        );

        // Get auth URL (from backend)
        $this->register_route(
            '/auth-url',
            'GET',
            array( $this, 'get_auth_url' ),
            array( $this, 'admin_access' )
        );

        // Disconnect
        $this->register_route(
            '/disconnect',
            'POST',
            array( $this, 'disconnect' ),
            array( $this, 'admin_access' )
        );

        // Save settings (calendar_id)
        $this->register_route(
            '/settings',
            'POST',
            array( $this, 'save_settings' ),
            array( $this, 'admin_access' )
        );

        // Get settings
        $this->register_route(
            '/settings',
            'GET',
            array( $this, 'get_settings' ),
            array( $this, 'admin_access' )
        );
    }

    /**
     * Get Google connection status.
     *
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response
     */
    public function get_status( $request ) {
        $service = new BoostBot_Google_Calendar_Service();
        $status  = $service->get_status();

        return $this->success(
            array(
                'configured'       => $status['configured'] ?? false,
                'connected'        => $status['connected'] ?? false,
                'email'            => $status['email'] ?? null,
                'using_constants'  => true, // Always true — backend manages credentials
            )
        );
    }

    /**
     * Get Google OAuth authorization URL from backend.
     *
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response
     */
    public function get_auth_url( $request ) {
        $service = new BoostBot_Google_Calendar_Service();
        $url = $service->get_auth_url();

        if ( empty( $url ) ) {
            return $this->error(
                __( 'Google Calendar is not configured on the server. Please contact support.', 'boostbot' ),
                500
            );
        }

        return $this->success(
            array(
                'url' => $url,
            )
        );
    }

    /**
     * Disconnect Google account.
     *
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response
     */
    public function disconnect( $request ) {
        $service = new BoostBot_Google_Calendar_Service();
        $service->disconnect();

        BoostBot_Logger::info( 'Google Calendar disconnected' );

        return $this->success(
            array(
                'message' => __( 'Google Calendar disconnected successfully', 'boostbot' ),
            )
        );
    }

    /**
     * Save Google Calendar settings (calendar_id only — no credentials needed).
     *
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response
     */
    public function save_settings( $request ) {
        $calendar_id = sanitize_text_field( $request->get_param( 'calendar_id' ) ?? 'primary' );

        $service = new BoostBot_Google_Calendar_Service();
        $service->save_settings( array(
            'calendar_id' => $calendar_id,
        ) );

        BoostBot_Logger::info( 'Google Calendar settings saved' );

        return $this->success(
            array(
                'message' => __( 'Settings saved successfully', 'boostbot' ),
            )
        );
    }

    /**
     * Get Google Calendar settings.
     *
     * @param WP_REST_Request $request Request object.
     * @return WP_REST_Response
     */
    public function get_settings( $request ) {
        $service = new BoostBot_Google_Calendar_Service();
        $status  = $service->get_status();

        return $this->success(
            array(
                'calendar_id' => $status['calendar_id'] ?? 'primary',
            )
        );
    }
}
