<?php

namespace GemBoards\hooks;

use GemBoards\Classes\EmailNotification;
use GemBoards\Emails\MentionUserComment;
use GemBoards\Emails\MentionUserDescription;
use GemBoards\Emails\NewComment;
use GemBoards\Emails\TaskAssignedUser;
use GemBoards\Emails\TaskRemovedUser;
use GemBoards\Emails\TaskStageChanged;
use GemBoards\Entities\Notification;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // if accessed directly, exit


class EmailNotifications {

	public static function init() {
		$self = new self();

		add_action( 'gemboards/notification/send_email', [ $self, 'send_email' ], 10, 2 );
	}

	public function send_email( Notification $notification, int $to_user_id ) {
		$emails = apply_filters('gemboards/notification/emails', [
			'assigned_user_to_task'    => TaskAssignedUser::class,
			'removed_user_from_task'   => TaskRemovedUser::class,
			'stage_changed'            => TaskStageChanged::class,
			'new_comment'              => NewComment::class,
			'mention_user_description' => MentionUserDescription::class,
			'mention_user_comment'     => MentionUserComment::class,
		]);

		if ( ! isset( $emails[ $notification->get_event_type() ] ) ) {
			return;
		}

		$user = get_user( $to_user_id );
		if ( ! $user ) {
			return;
		}

		$email_class = $emails[ $notification->get_event_type() ];
		/** @var EmailNotification $email_class */
		( new $email_class( $notification, $user ) )->send_mail();
	}

}
