<?php

namespace FluentPlayer\App\Http\Controllers;

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

class SmartcodeController extends Controller
{
    /**
     * Get available smartcodes (shortcodes) for use in text fields.
     *
     * @return \WP_REST_Response
     */
    public function get()
    {
        try {
            if ( ! defined( 'FLUENTCRM' ) ) {
                return $this->sendSuccess([
                    'has_fluentcrm' => false,
                    'smartcodes'    => [],
                    'install_url'   => admin_url( 'plugin-install.php?tab=search&type=term&s=fluentcrm' ),
                ]);
            }

            $smartcodes = \FluentCrm\App\Services\Helper::getGlobalSmartCodes();

            $smartcodes = apply_filters( 'fluent_player/smartcode_groups', $smartcodes );

            return $this->sendSuccess([
                'has_fluentcrm' => true,
                'smartcodes'    => $smartcodes,
            ]);
        } catch ( \Exception $e ) {
            return $this->sendError([
                'message' => $e->getMessage(),
            ], 400);
        }
    }
}
