<?php

namespace EasyLinkAutomations\Triggers\WordPress;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a specific user meta key is updated.
 */
final class UserMetaUpdatedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'wordpress/user_meta_updated';
    }

    public static function getName(): string
    {
        return __('User meta key updated', 'easylink-automations');
    }

    public static function getDescription(): string
    {
        return __('Triggers when a specific user meta key is updated', 'easylink-automations');
    }

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

    public static function getIcon(): string
    {
        return 'user-add';
    }

    public static function getOutputFields(): array
    {
        return [
            'user_id'      => ['label' => 'User ID', 'type' => 'number'],
            'user_login'   => ['label' => 'Username', 'type' => 'string'],
            'user_email'   => ['label' => 'User Email', 'type' => 'email'],
            'display_name' => ['label' => 'Display Name', 'type' => 'string'],
            'first_name'   => ['label' => 'First Name', 'type' => 'string'],
            'last_name'    => ['label' => 'Last Name', 'type' => 'string'],
            'role'         => ['label' => 'Role', 'type' => 'string'],
            'meta_key'     => ['label' => 'Meta Key', 'type' => 'string'],
            'meta_value'   => ['label' => 'Meta Value', 'type' => 'string'],
        ];
    }

    public static function getConfigFields(): array
    {
        return [
            'meta_key' => [
                'label'            => 'Meta Key',
                'type'             => 'text',
                'required'         => true,
                'description'      => 'The user meta key to watch for updates',
                'supports_mapping' => false,
            ],
        ];
    }

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

        add_action('updated_user_meta', function (int $metaId, int $userId, string $metaKey, mixed $metaValue): void {
            $data = $this->handle($metaId, $userId, $metaKey, $metaValue);
            $this->fire($data);
        }, 10, 4);
    }

    public function handle(mixed ...$args): array
    {
        $userId    = (int) $args[1];
        $metaKey   = (string) $args[2];
        $metaValue = $args[3];

        $metaValue = maybe_unserialize($metaValue);
        if (!is_string($metaValue)) {
            $metaValue = (string) $metaValue;
        }

        $user  = get_userdata($userId);
        $roles = $user->roles ?? [];

        return [
            'user_id'      => $userId,
            'user_login'   => $user->user_login ?? '',
            'user_email'   => $user->user_email ?? '',
            'display_name' => $user->display_name ?? '',
            'first_name'   => $user->first_name ?? '',
            'last_name'    => $user->last_name ?? '',
            'role'         => $roles[0] ?? '',
            'meta_key'     => $metaKey,
            'meta_value'   => $metaValue,
        ];
    }
}
