<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class EmailTemplate extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'content',
        'created_by',
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function creator()
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function attachments()
    {
        return $this->hasMany(EmailTemplateAttachment::class, 'attachments');
    }

    public function scheduledEmails()
    {
        return $this->hasMany(EmailScheduled::class, 'template_id');
    }

    public function getWordCountAttribute()
    {
        return str_word_count(strip_tags($this->content));
    }

    public function getPreviewAttribute()
    {
        return Str::limit(strip_tags($this->content), 100);
    }
}
