<?php

namespace App\Models;

use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * One answer, written once and reused.
 *
 * Most customer questions are the same five questions, and typing the answer
 * again every time is how two customers get told two different things about the
 * same subject.
 *
 * A template is inserted into the compose field, never sent by itself: the
 * placeholders come out filled from the customer's record and the last two
 * sentences are still the operator's to write. See MailTemplateRenderer for the
 * placeholders and why they are named in English.
 */
class MailTemplate extends Model
{
    use HasFactory;
    use HasUuid;

    protected $fillable = ['name', 'subject', 'body', 'sort', 'active'];

    protected function casts(): array
    {
        return [
            'sort' => 'integer',
            'active' => 'boolean',
        ];
    }

    /** In the order an operator thinks in, which is what `sort` is for. */
    public function scopeUsable(Builder $query): Builder
    {
        return $query->where('active', true)->orderBy('sort')->orderBy('name');
    }
}
