<?php
declare(strict_types=1);

namespace App\Modules\Notifications\Models;

use App\Core\App;

class SmsTemplate
{
    public static function getAll(): array
    {
        $db = App::getInstance()->db();
        return $db->select("SELECT * FROM sms_templates ORDER BY name_ar");
    }

    public static function findByCode(string $code): ?array
    {
        $db = App::getInstance()->db();
        return $db->selectOne("SELECT * FROM sms_templates WHERE template_code = ? AND is_active = 1", [$code]);
    }

    public static function findByEvent(string $event): ?array
    {
        $db = App::getInstance()->db();
        return $db->selectOne("SELECT * FROM sms_templates WHERE trigger_event = ? AND is_active = 1", [$event]);
    }

    public static function renderMessage(string $template, array $variables): string
    {
        $message = $template;
        foreach ($variables as $key => $value) {
            $message = str_replace('{' . $key . '}', (string) $value, $message);
        }
        return $message;
    }
}