<?php
declare(strict_types=1);

namespace App\Services;

use App\Core\Database;

final class NotificationService
{
    public static function notify(int $userId, string $type, string $message, ?string $linkUrl = null): void
    {
        $db = Database::getInstance();
        $db->prepare("INSERT INTO notifications(user_id,type,message,link_url,is_read) VALUES(:u,:t,:m,:l,0)")
           ->execute([':u'=>$userId, ':t'=>$type, ':m'=>$message, ':l'=>$linkUrl]);
    }

    public static function notifyEmail(int $userId, string $templateKey, string $lang, array $vars): void
    {
        $db = Database::getInstance();
        $st = $db->prepare("SELECT email,first_name,last_name FROM users WHERE id=:id LIMIT 1");
        $st->execute([':id'=>$userId]);
        $u = $st->fetch() ?: [];
        $to = (string)($u['email'] ?? '');
        if ($to === '') return;
        $name = trim(((string)($u['first_name'] ?? '')).' '.((string)($u['last_name'] ?? '')));
        EmailTemplateService::queue($to, $name, $templateKey, $lang, $vars);
    }
}
