<?php

namespace EasyLinkAutomations\Triggers\LearnDash;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a user completes a LearnDash lesson.
 */
final class LessonCompletedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'learndash/lesson_completed';
    }

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

    public static function getDescription(): string
    {
        return __('Fires when a user completes a LearnDash lesson', 'easylink-automations');
    }

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

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


    public static function isAvailable(): bool
    {
        return class_exists('SFWD_LMS');
    }

    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'],
            'first_name'   => ['label' => 'First Name', 'type' => 'string'],
            'last_name'    => ['label' => 'Last Name', 'type' => 'string'],
            'display_name' => ['label' => 'Display Name', 'type' => 'string'],
            'course_id'    => ['label' => 'Course ID', 'type' => 'number'],
            'course_title' => ['label' => 'Course Title', 'type' => 'string'],
            'course_url'   => ['label' => 'Course URL', 'type' => 'url'],
            'lesson_id'    => ['label' => 'Lesson ID', 'type' => 'number'],
            'lesson_title' => ['label' => 'Lesson Title', 'type' => 'string'],
            'lesson_url'   => ['label' => 'Lesson URL', 'type' => 'url'],
            'completed_at' => ['label' => 'Completed At', 'type' => 'datetime'],
        ];
    }

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

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

        add_action('learndash_lesson_completed', function (array $data): void {
            $result = $this->handle($data);
            if (empty($result)) {
                return;
            }
            $this->fire($result);
        }, 10, 1);
    }

    public function handle(mixed ...$args): array
    {
        $data   = $args[0] ?? [];
        $user   = $data['user'] ?? null;
        $course = $data['course'] ?? null;
        $lesson = $data['lesson'] ?? null;

        if (!$user instanceof \WP_User || !$course instanceof \WP_Post || !$lesson instanceof \WP_Post) {
            return [];
        }

        return [
            'user_id'      => $user->ID,
            'user_login'   => $user->user_login,
            'user_email'   => $user->user_email,
            'first_name'   => $user->first_name,
            'last_name'    => $user->last_name,
            'display_name' => $user->display_name,
            'course_id'    => $course->ID,
            'course_title' => $course->post_title,
            'course_url'   => get_permalink($course->ID) ?: '',
            'lesson_id'    => $lesson->ID,
            'lesson_title' => $lesson->post_title,
            'lesson_url'   => get_permalink($lesson->ID) ?: '',
            'completed_at' => date_i18n('Y-m-d H:i:s'),
        ];
    }
}
