<?php

// app/Models/NotificationTemplate.php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class NotificationTemplate extends Model
{
    use HasFactory;

    protected $fillable = [
        'code', 'name', 'type', 'subject', 'content', 'variables', 'is_active',
    ];

    protected $casts = [
        'variables' => 'array',
        'is_active' => 'boolean',
    ];

    public function render(array $data = [])
    {
        $content = $this->content;
        $subject = $this->subject;

        foreach ($data as $key => $value) {
            $content = str_replace("{{$key}}", $value, $content);
            $subject = str_replace("{{$key}}", $value, $subject);
        }

        return [
            'subject' => $subject,
            'content' => $content,
        ];
    }
}
