<?php

namespace EasyLinkAutomations\Triggers\ElementorPro;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a post is published using Elementor.
 */
final class PostPublishedWithElementorTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'elementor-pro/post_published';
    }

    public static function getName(): string
    {
        return __('Post Published with Elementor', 'easylink-automations');
    }

    public static function getDescription(): string
    {
        return __('Fires when a post is published using Elementor.', 'easylink-automations');
    }

    public static function getCategory(): string
    {
        return 'Elementor Pro';
    }

    public static function getIcon(): string
    {
        return 'elementor-pro';
    }

    public static function isAvailable(): bool
    {
        return class_exists('ElementorPro\Plugin');
    }

    public static function getOutputFields(): array
    {
        return [
            'post_id'          => ['label' => 'Post ID', 'type' => 'number'],
            'post_title'       => ['label' => 'Post Title', 'type' => 'string'],
            'post_type'        => ['label' => 'Post Type', 'type' => 'string'],
            'post_url'         => ['label' => 'Post URL', 'type' => 'url'],
            'post_status'      => ['label' => 'Post Status', 'type' => 'string'],
            'post_author_id'   => ['label' => 'Author ID', 'type' => 'number'],
            'post_author_name' => ['label' => 'Author Name', 'type' => 'string'],
        ];
    }

    public static function getConfigFields(): array
    {
        return [];
    }

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

        add_action('transition_post_status', function (string $newStatus, string $oldStatus, \WP_Post $post): void {
            if ($newStatus === $oldStatus) {
                return;
            }
            if ('publish' !== $newStatus) {
                return;
            }
            if (wp_is_post_revision($post->ID) || wp_is_post_autosave($post->ID)) {
                return;
            }
            if (empty(get_post_meta($post->ID, '_elementor_edit_mode', true))) {
                return;
            }
            $data = $this->handle($newStatus, $oldStatus, $post);
            if (empty($data)) {
                return;
            }
            $this->fire($data);
        }, 10, 3);
    }

    public function handle(mixed ...$args): array
    {
        [, , $post] = $args;

        $author = get_userdata((int) $post->post_author);

        return [
            'post_id'          => $post->ID,
            'post_title'       => $post->post_title,
            'post_type'        => $post->post_type,
            'post_url'         => get_permalink($post->ID) ?: '',
            'post_status'      => $post->post_status,
            'post_author_id'   => (int) $post->post_author,
            'post_author_name' => $author ? $author->display_name : '',
        ];
    }
}
