<?php

namespace BitApps\BTCBI_PRO\Triggers\SiteOriginWidgets;

use BitApps\BTCBI_PRO\Triggers\TriggerController;
use BitCode\FI\Flow\Flow;

final class SiteOriginWidgetsController
{
    public static function info()
    {
        return [
            'name'                => 'SiteOrigin Widgets',
            'title'               => 'SiteOrigin Widgets',
            'type'                => 'custom_form_submission',
            'is_active'           => true,
            'documentation_url'   => '',
            'tutorial_url'        => '',
            'triggered_entity_id' => 'siteorigin_widgets_contact_sent', // Form submission hook act as triggered_entity_id
            'fetch'               => [
                'action' => 'siteoriginwidgets/test',
                'method' => 'post',
            ],
            'fetch_remove' => [
                'action' => 'siteoriginwidgets/test/remove',
                'method' => 'post',
            ],
            'isPro' => true
        ];
    }

    public function getTestData()
    {
        return TriggerController::getTestData('site_origin_widgets');
    }

    public function removeTestData($data)
    {
        return TriggerController::removeTestData($data, 'site_origin_widgets');
    }

    public static function handleSiteOriginWidgetsSubmit(...$record)
    {
        $formData = SiteOriginWidgetsHelper::setFields($record[0]['fields'], $record[0]['_sow_form_id'], $record[1]);

        if (get_option('btcbi_site_origin_widgets_test') !== false) {
            update_option('btcbi_site_origin_widgets_test', [
                'formData'   => $formData,
                'primaryKey' => [(object) ['key' => 'id', 'value' => $record[0]['_sow_form_id']]]
            ]);
        }

        if ($flows = Flow::exists('SiteOriginWidgets', current_action())) {
            $formIdField = ['id' => $record[0]['_sow_form_id']];

            foreach ($flows as $flow) {
                $flowDetails = static::parseFlowDetails($flow->flow_details);

                if (!isset($flowDetails->primaryKey)) {
                    continue;
                }

                if (SiteOriginWidgetsHelper::isPrimaryKeysMatch($record[1], $flowDetails, $formIdField)) {
                    $data = SiteOriginWidgetsHelper::prepareDataForFlow($record[1], $formIdField);
                    Flow::execute('SiteOriginWidgets', current_action(), $data, [$flow]);
                }
            }
        }

        return ['type' => 'success'];
    }

    private static function parseFlowDetails($flowDetails)
    {
        return \is_string($flowDetails) ? json_decode($flowDetails) : $flowDetails;
    }
}
