<?php

namespace EasyLinkAutomations\Triggers\GravityForms;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

final class FormSubmittedWithSpecificFieldTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'gravityforms/form_submitted_specific_field';
    }

    public static function getName(): string
    {
        return __('Form Submitted with Specific Field Value', 'easylink-automations');
    }

    public static function getDescription(): string
    {
        return __('Fires when a Gravity Forms form is submitted, once per field with its value.', 'easylink-automations');
    }

    public static function getCategory(): string
    {
        return 'Gravity Forms';
    }

    public static function getIcon(): string
    {
        return 'gravity-form';
    }

    public static function isAvailable(): bool
    {
        return class_exists('GFAPI');
    }

    public static function getOutputFields(): array
    {
        return [
            ['key' => 'entry_id', 'label' => 'Entry ID', 'type' => 'integer'],
            ['key' => 'form_id', 'label' => 'Form ID', 'type' => 'integer'],
            ['key' => 'form_title', 'label' => 'Form Title', 'type' => 'string'],
            ['key' => 'field_id', 'label' => 'Field ID', 'type' => 'string'],
            ['key' => 'field_label', 'label' => 'Field Label', 'type' => 'string'],
            ['key' => 'field_value', 'label' => 'Field Value', 'type' => 'string'],
        ];
    }

    public static function getConfigFields(): array
    {
        return [
            'form_id' => [
                'label'    => 'Select Form',
                'type'     => 'select',
                'options'  => static::getAvailableForms(),
                'required' => true,
            ],
        ];
    }

    private static function getAvailableForms(): array
    {
        $options = [];

        if (class_exists('GFAPI')) {
            $forms = \GFAPI::get_forms();
            if (is_array($forms)) {
                foreach ($forms as $form) {
                    $options[] = [
                        'value' => (string) $form['id'],
                        'label' => $form['title'] ?? ('Form #' . $form['id']),
                    ];
                }
            }
        }

        return $options;
    }

    public function register(callable $callback): void
    {
        $this->callback = $callback;

        add_action('gform_after_submission', function ($entry, $form): void {
            if (isset($entry['status']) && 'spam' === $entry['status']) {
                return;
            }

            if (empty($form['fields'])) {
                return;
            }

            foreach ($form['fields'] as $field) {
                $field_id    = $field->id;
                $field_value = isset($entry[$field_id]) ? $entry[$field_id] : '';

                if (empty($field_value)) {
                    continue;
                }

                $data = [
                    'entry_id'    => $entry['id'] ?? '',
                    'form_id'     => $entry['form_id'] ?? '',
                    'form_title'  => $form['title'] ?? '',
                    'date_created' => $entry['date_created'] ?? '',
                    'source_url'  => $entry['source_url'] ?? '',
                    'ip'          => $entry['ip'] ?? '',
                    'field_id'    => $field_id,
                    'field_label' => $field->label,
                    'field_value' => $field_value,
                ];

                $this->fire($data);
            }
        }, 10, 2);
    }

    public function handle(mixed ...$args): array
    {
        return [];
    }
}
