<?php

namespace App\Models;

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

class ReminderTemplate extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'content',
        'channel',
        'hours_before',
        'is_active',
    ];

    protected $casts = [
        'hours_before' => 'integer',
        'is_active' => 'boolean',
    ];

    public function reminders(): HasMany
    {
        return $this->hasMany(Reminder::class, 'template_id');
    }

    public function parseContent(array $placeholders): string
    {
        $content = $this->content;
        foreach ($placeholders as $key => $value) {
            $content = str_replace('{' . $key . '}', $value, $content);
        }
        return $content;
    }
}
