<?php

namespace EasyLinkAutomations\Triggers\TutorLMS;

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

use EasyLinkAutomations\Triggers\BaseTrigger;

/**
 * Trigger that fires when a user enrolls in a TutorLMS course.
 */
final class UserEnrolledCourseTrigger extends BaseTrigger
{
    public static function getSlug(): string
    {
        return 'tutorlms/user_enrolled_course';
    }

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

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

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

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


    public static function isAvailable(): bool
    {
        return class_exists('\TUTOR\Tutor');
    }

    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'],
            'post_title'   => ['label' => 'Course Title', 'type' => 'string'],
        ];
    }

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

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

        add_action('tutor_after_enrolled', function (int $course_id, int $user_id, mixed $is_enrolled): void {
            $result = $this->handle($course_id, $user_id, $is_enrolled);
            if (empty($result)) {
                return;
            }
            $this->fire($result);
        }, 10, 3);
    }

    public function handle(mixed ...$args): array
    {
        $course_id = (int) ($args[0] ?? 0);
        $user_id   = (int) ($args[1] ?? 0);

        if (!$course_id || !$user_id) {
            return [];
        }

        $user = get_userdata($user_id);
        if (!$user instanceof \WP_User) {
            return [];
        }

        $course = get_post($course_id);

        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,
            'post_title'   => $course instanceof \WP_Post ? $course->post_title : '',
            'post_status'  => $course instanceof \WP_Post ? $course->post_status : '',
            'post_type'    => $course instanceof \WP_Post ? $course->post_type : '',
        ];
    }
}
