<?php

namespace EasyLinkAutomations\HTTP\Controllers\Triggers;

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

use EasyLinkAutomations\HTTP\Request\Request;
use EasyLinkAutomations\HTTP\Response;

/**
 * Controller for Formidable Forms trigger endpoints.
 */
final class FormidableFormsController
{
    /**
     * Return form fields for a specific Formidable Forms form.
     *
     * Route: formidable-forms/form-fields
     */
    public function formFields(Request $request): Response
    {
        $formId = (int) $request->get('form_id', 0);
        if ($formId <= 0) {
            return Response::error('form_id is required', 400);
        }

        if (!class_exists('FrmField')) {
            return Response::error('Formidable Forms is not active', 400);
        }

        $fields = [
            'form_id'    => 'Form ID',
            'entry_id'   => 'Entry ID',
            'user_id'    => 'User ID',
            'user_login' => 'Username',
            'user_email' => 'User Email',
        ];

        $formFields = \FrmField::get_all_for_form($formId, '', 'include');
        if (is_array($formFields)) {
            foreach ($formFields as $field) {
                $fields[$field->name] = $field->name;
            }
        }

        return Response::success(['fields' => $fields]);
    }
}
