<?php

namespace PloverCRM\Core\Api\V1;

use PloverCRM\Core\Api\AbstractEndpoint;
use WP_REST_Request;

/**
 * Custom Fields Endpoint
 * 
 * GET /wp-json/plover-crm/v1/custom-fields
 */
class CustomFieldsEndpoint extends AbstractEndpoint
{
    public function getRoute(): string
    {
        return '/custom-fields';
    }

    public function getMethod()
    {
        return 'GET';
    }

    public function handle(WP_REST_Request $request)
    {
        $crm = $this->container->get('crm');

        if (!$crm->isAvailable()) {
            return $this->error('CRM not available', 503);
        }

        $fields = $crm->getAllCustomFields();

        return $this->success($fields, [
            'total' => count($fields),
        ]);
    }
}
