<?php

namespace EasyLinkAutomations\HTTP\Controllers\Triggers;

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

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

/**
 * Controller for GiveWP trigger endpoints.
 */
final class GiveWPController
{
    /**
     * Return form fields for a specific GiveWP donation form.
     *
     * Route: givewp/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('Give_Payment')) {
            return Response::error('GiveWP is not active', 400);
        }

        $form = get_post($formId);
        if (!$form || $form->post_type !== 'give_forms') {
            return Response::error('Form not found', 404);
        }

        // GiveWP donation fields are standardized across all forms.
        $fields = [
            'form_id'           => 'Form ID',
            'form_title'        => 'Form Title',
            'first_name'        => 'First Name',
            'last_name'         => 'Last Name',
            'email'             => 'Email',
            'currency'          => 'Currency',
            'donation_amount'   => 'Donation Amount',
            'name_title_prefix' => 'Title Prefix',
            'date'              => 'Date',
            'address_line_1'    => 'Address Line 1',
            'address_line_2'    => 'Address Line 2',
            'city'              => 'City',
            'state'             => 'State',
            'zip'               => 'ZIP',
            'country'           => 'Country',
            'payment_meta'      => 'Payment Meta',
            'comment'           => 'Donor Comment',
        ];

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