<?php

namespace EasyLinkAutomations\Triggers\WordPress;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a post is published.
 */
final class PostPublishedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'wordpress/post_published';
    }

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

    public static function getDescription(): string
    {
        return __('Triggers when a post is published for the first time', 'easylink-automations');
    }

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

    public static function getIcon(): string
    {
        return 'post';
    }

    public static function getOutputFields(): array
    {
        return [
            'post_id'      => ['label' => 'Post ID', 'type' => 'number'],
            'post_title'   => ['label' => 'Post Title', 'type' => 'string'],
            'post_content' => ['label' => 'Post Content', 'type' => 'html'],
            'post_excerpt' => ['label' => 'Post Excerpt', 'type' => 'string'],
            'post_url'     => ['label' => 'Post URL', 'type' => 'url'],
            'post_type'    => ['label' => 'Post Type', 'type' => 'string'],
            'post_author'  => ['label' => 'Author ID', 'type' => 'number'],
            'author_name'  => ['label' => 'Author Name', 'type' => 'string'],
            'author_email' => ['label' => 'Author Email', 'type' => 'email'],
            'post_date'    => ['label' => 'Publish Date', 'type' => 'datetime'],
            'categories'   => ['label' => 'Categories', 'type' => 'array'],
            'tags'         => ['label' => 'Tags', 'type' => 'array'],
        ];
    }

    public static function getConfigFields(): array
    {
        return [
            'post_type' => [
                'label'   => 'Post Type',
                'type'    => 'select',
                'options' => 'dynamic:post_types',
                'default' => 'post',
            ],
            'categories' => [
                'label'   => 'Only these categories (optional)',
                'type'    => 'multi_select',
                'options' => 'dynamic:categories',
            ],
        ];
    }

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

        add_action('transition_post_status', function ($newStatus, $oldStatus, $post): void {
            // Only trigger on first publish
            if ($newStatus !== 'publish' || $oldStatus === 'publish') {
                return;
            }

            // Skip revisions and autosaves
            if (wp_is_post_revision($post->ID) || wp_is_post_autosave($post->ID)) {
                return;
            }

            $data = $this->handle($post);
            $this->fire($data);
        }, 10, 3);
    }

    public function handle(mixed ...$args): array
    {
        $post   = $args[0];
        $author = get_userdata($post->post_author);

        return [
            'post_id'      => $post->ID,
            'post_title'   => $post->post_title,
            'post_content' => $post->post_content,
            'post_excerpt' => $post->post_excerpt,
            'post_url'     => get_permalink($post->ID),
            'post_type'    => $post->post_type,
            'post_author'  => $post->post_author,
            'author_name'  => $author->display_name ?? '',
            'author_email' => $author->user_email ?? '',
            'post_date'    => $post->post_date,
            'categories'   => wp_get_post_categories($post->ID, ['fields' => 'names']),
            'tags'         => wp_get_post_tags($post->ID, ['fields' => 'names']),
        ];
    }
}
