<?php

namespace EasyLinkAutomations\Triggers\WordPress;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a user's comment on a post is approved.
 */
final class CommentApprovedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'wordpress/comment_approved';
    }

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

    public static function getDescription(): string
    {
        return __('Triggers when a user\'s comment on a post is approved', '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' => 'string'],
            'comment_author'       => ['label' => 'Comment Author', 'type' => 'string'],
            'comment_author_email' => ['label' => 'Comment Author Email', 'type' => 'email'],
            'comment_date'         => ['label' => 'Comment Date', 'type' => 'datetime'],
            'comment_url'          => ['label' => 'Comment URL', 'type' => 'url'],
            'post_id'              => ['label' => 'Post ID', 'type' => 'number'],
            'post_title'           => ['label' => 'Post Title', 'type' => 'string'],
            'post_url'             => ['label' => 'Post URL', 'type' => 'url'],
            'post_author'          => ['label' => 'Post Author', 'type' => 'string'],
            'user_id'              => ['label' => 'Commenter User ID', 'type' => 'number'],
            'user_login'           => ['label' => 'Commenter User Login', 'type' => 'string'],
            'user_email'           => ['label' => 'Commenter User Email', 'type' => 'email'],
            'display_name'         => ['label' => 'Commenter Display Name', 'type' => 'string'],
            'first_name'           => ['label' => 'Commenter First Name', 'type' => 'string'],
            'last_name'            => ['label' => 'Commenter Last Name', 'type' => 'string'],
            'role'                 => ['label' => 'Commenter Role', 'type' => 'string'],
        ];
    }

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

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

        add_action('transition_comment_status', function (string $newStatus, string $oldStatus, \WP_Comment $comment): void {
            if ($newStatus !== 'approved') {
                return;
            }

            $post = get_post($comment->comment_post_ID);
            if (!$post instanceof \WP_Post) {
                return;
            }

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

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

        $postAuthorData = get_userdata($post->post_author);
        $postAuthor     = $postAuthorData->display_name ?? '';

        $userId    = (int) $comment->user_id;
        $userData  = $userId > 0 ? get_userdata($userId) : null;
        $roles     = $userData->roles ?? [];

        return [
            'comment_id'           => (int) $comment->comment_ID,
            'comment_content'      => $comment->comment_content,
            'comment_author'       => $comment->comment_author,
            'comment_author_email' => $comment->comment_author_email,
            'comment_date'         => $comment->comment_date,
            'comment_url'          => get_comment_link($comment->comment_ID),
            'post_id'              => (int) $comment->comment_post_ID,
            'post_title'           => $post->post_title,
            'post_url'             => get_permalink($post->ID),
            'post_author'          => $postAuthor,
            'user_id'              => $userId,
            'user_login'           => $userData->user_login ?? '',
            'user_email'           => $userData->user_email ?? '',
            'display_name'         => $userData->display_name ?? '',
            'first_name'           => $userData->first_name ?? '',
            'last_name'            => $userData->last_name ?? '',
            'role'                 => $roles[0] ?? '',
        ];
    }
}
