<?php

namespace EasyLinkAutomations\Triggers\WordPress;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a comment is added.
 */
final class CommentAddedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'wordpress/comment_added';
    }

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

    public static function getDescription(): string
    {
        return __('Triggers when a new comment is posted', 'easylink-automations');
    }

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

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

    public static function getOutputFields(): array
    {
        return [
            'comment_id'      => ['label' => 'Comment ID', 'type' => 'number'],
            'comment_content' => ['label' => 'Comment Content', 'type' => 'html'],
            'comment_author'  => ['label' => 'Author Name', 'type' => 'string'],
            'comment_email'   => ['label' => 'Author Email', 'type' => 'email'],
            'comment_url'     => ['label' => 'Author Website', 'type' => 'url'],
            'comment_date'    => ['label' => 'Comment Date', 'type' => 'datetime'],
            'post_id'         => ['label' => 'Post ID', 'type' => 'number'],
            'post_title'      => ['label' => 'Post Title', 'type' => 'string'],
            'post_url'        => ['label' => 'Post URL', 'type' => 'url'],
            'is_approved'     => ['label' => 'Is Approved', 'type' => 'boolean'],
        ];
    }

    public static function getConfigFields(): array
    {
        return [
            'only_approved' => [
                'label'   => 'Only approved comments',
                'type'    => 'toggle',
                'default' => false,
            ],
            'post_type' => [
                'label'   => 'Post Type',
                'type'    => 'select',
                'options' => 'dynamic:post_types',
                'default' => 'post',
            ],
        ];
    }

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

        add_action('wp_insert_comment', function ($commentId, $comment): void {
            // Get the post for this comment
            $post = get_post($comment->comment_post_ID);
            if (!$post) {
                return;
            }

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

    public function handle(mixed ...$args): array
    {
        $comment = $args[0];
        $post    = $args[1] ?? get_post($comment->comment_post_ID);

        return [
            'comment_id'      => $comment->comment_ID,
            'comment_content' => $comment->comment_content,
            'comment_author'  => $comment->comment_author,
            'comment_email'   => $comment->comment_author_email,
            'comment_url'     => $comment->comment_author_url,
            'comment_date'    => $comment->comment_date,
            'post_id'         => $comment->comment_post_ID,
            'post_title'      => $post->post_title ?? '',
            'post_url'        => get_permalink($comment->comment_post_ID),
            'is_approved'     => $comment->comment_approved === '1',
        ];
    }
}
