<?php

namespace PloverCRM\Modules\Automations\Services;

/**
 * Built-in automation templates.
 */
class AutomationTemplateRegistry
{
    public static function getTemplates(): array
    {
        return [
            [
                'key' => 'new_contact_alert',
                'name' => 'New Contact Alert',
                'category' => 'Email',
                'description' => 'Send an internal email whenever a new contact is created and enriched.',
                'builder_mode' => 'guided_email',
                'definition' => [
                    'version' => 1,
                    'meta' => [],
                    'trigger' => [
                        'event' => 'contact_created_after_enrichment',
                    ],
                    'conditions' => [],
                    'actions' => [
                        [
                            'type' => 'send_internal_email',
                            'config' => [
                                'to' => [],
                                'subject' => 'New contact: {full_name}',
                                'body' => '<p>A new contact was created in <strong>{site_name}</strong>.</p><p><strong>Name:</strong> {full_name}<br><strong>Email:</strong> {email}<br><strong>Phone:</strong> {phone}<br><strong>Source:</strong> {source}<br><strong>Lists:</strong> {lists}<br><strong>Tags:</strong> {tags}</p><p><strong>Workspace:</strong> {workspace}<br><strong>Time:</strong> {datetime}</p>',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'key' => 'webhook_lead_alert',
                'name' => 'Webhook Lead Alert',
                'category' => 'Email',
                'description' => 'Notify your team when a webhook lead arrives.',
                'builder_mode' => 'guided_email',
                'definition' => [
                    'version' => 1,
                    'meta' => [],
                    'trigger' => [
                        'event' => 'contact_created_after_enrichment',
                    ],
                    'conditions' => [
                        [
                            'field' => 'contact.source',
                            'operator' => 'equals',
                            'value' => 'Webhook',
                        ],
                    ],
                    'actions' => [
                        [
                            'type' => 'send_internal_email',
                            'config' => [
                                'to' => [],
                                'subject' => 'Webhook lead received: {full_name}',
                                'body' => '<p>A webhook lead was added to <strong>{site_name}</strong>.</p><p><strong>Name:</strong> {full_name}<br><strong>Email:</strong> {email}<br><strong>Phone:</strong> {phone}<br><strong>Lists:</strong> {lists}<br><strong>Tags:</strong> {tags}</p><p><strong>Time:</strong> {datetime}</p>',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'key' => 'list_lead_alert',
                'name' => 'List Lead Alert',
                'category' => 'Email',
                'description' => 'Start with this template when you want one alert rule for one or more CRM lists.',
                'builder_mode' => 'guided_email',
                'definition' => [
                    'version' => 1,
                    'meta' => [],
                    'trigger' => [
                        'event' => 'contact_created_after_enrichment',
                    ],
                    'conditions' => [],
                    'actions' => [
                        [
                            'type' => 'send_internal_email',
                            'config' => [
                                'to' => [],
                                'subject' => 'List alert: {full_name}',
                                'body' => '<p>A new contact matched your list automation.</p><p><strong>Name:</strong> {full_name}<br><strong>Email:</strong> {email}<br><strong>Lists:</strong> {lists}<br><strong>Tags:</strong> {tags}</p><p><strong>Time:</strong> {datetime}</p>',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'key' => 'google_ads_stage_conversion',
                'name' => 'Google Ads Stage Conversion',
                'category' => 'Conversions',
                'description' => 'Send an offline Google Ads conversion when a contact moves into a selected CRM stage.',
                'builder_mode' => 'guided_google_ads',
                'definition' => [
                    'version' => 1,
                    'meta' => [],
                    'trigger' => [
                        'event' => 'contact_stage_changed',
                    ],
                    'conditions' => [
                        [
                            'field' => 'context.stage.to',
                            'operator' => 'equals',
                            'value' => '',
                        ],
                    ],
                    'actions' => [
                        [
                            'type' => 'send_google_ads_conversion',
                            'config' => [
                                'connection_id' => 0,
                                'conversion_action_id' => '',
                                'conversion_action_name' => '',
                                'conversion_value' => 1,
                                'currency_code' => 'USD',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'key' => 'meta_stage_event',
                'name' => 'Meta Stage Event',
                'category' => 'Conversions',
                'description' => 'Send a Meta Conversions API event when a contact moves into a selected CRM stage.',
                'builder_mode' => 'guided_meta_capi',
                'definition' => [
                    'version' => 1,
                    'meta' => [],
                    'trigger' => [
                        'event' => 'contact_stage_changed',
                    ],
                    'conditions' => [
                        [
                            'field' => 'context.stage.to',
                            'operator' => 'equals',
                            'value' => '',
                        ],
                    ],
                    'actions' => [
                        [
                            'type' => 'send_meta_capi_event',
                            'config' => [
                                'connection_id' => 0,
                                'event_name' => 'Lead',
                                'custom_event_name' => '',
                                'custom_data' => [],
                                'custom_data_parameters' => [],
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }

    public static function getTemplate(string $templateKey): ?array
    {
        foreach (self::getTemplates() as $template) {
            if ($template['key'] === $templateKey) {
                return $template;
            }
        }

        return null;
    }
}
