<?php

namespace EasyLinkAutomations\Triggers\AcademyLMS;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a user completes a course in Academy LMS.
 */
final class CourseCompletedTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'academylms/course_completed';
    }

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

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

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

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


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

    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'],
            'user_role'     => ['label' => 'User Role', 'type' => 'string'],
            'course_id'     => ['label' => 'Course ID', 'type' => 'number'],
            'course_title'  => ['label' => 'Course Title', 'type' => 'string'],
            'course_url'    => ['label' => 'Course URL', 'type' => 'url'],
            'course_status' => ['label' => 'Course Status', 'type' => 'string'],
            'course_author' => ['label' => 'Course Author', 'type' => 'string'],
        ];
    }

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

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

        add_action('academy/admin/course_complete_after', function (int $courseId, int $userId): void {
            if (empty($userId)) {
                return;
            }
            $data = $this->handle($courseId, $userId);
            if (empty($data)) {
                return;
            }
            $this->fire($data);
        }, 10, 2);
    }

    public function handle(mixed ...$args): array
    {
        [$courseId, $userId] = $args;

        $user   = get_userdata((int) $userId);
        $course = get_post((int) $courseId);

        if (!$user || !$course) {
            return [];
        }

        $roles        = $user->roles ?? [];
        $courseAuthor = get_userdata((int) $course->post_author);

        return [
            'user_id'       => $user->ID,
            'user_login'    => $user->user_login,
            'user_email'    => $user->user_email,
            'display_name'  => $user->display_name,
            'first_name'    => get_user_meta($user->ID, 'first_name', true),
            'last_name'     => get_user_meta($user->ID, 'last_name', true),
            'user_role'     => $roles[0] ?? '',
            'course_id'     => $course->ID,
            'course_title'  => $course->post_title,
            'course_url'    => get_permalink($course->ID) ?: '',
            'course_status' => $course->post_status,
            'course_author' => $courseAuthor ? $courseAuthor->display_name : '',
        ];
    }
}
