<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * The answers an operator gives over and over, written once.
 *
 * Most customer questions are the same five questions. Typing the answer again
 * every time is how two customers get told two different things about the same
 * subject, and how the third one gets a sentence that is nearly right.
 *
 * A template is text with placeholders in it. It is INSERTED into the compose
 * field with the placeholders already filled from the customer's own record —
 * not sent as it stands, because the last two sentences are always specific to
 * the person asking. What goes out is what the operator saw and edited.
 *
 * `sort` exists so the list reads in the order the operator thinks in, which is
 * neither alphabetical nor the order they happened to be written in.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('mail_templates', function (Blueprint $table) {
            $table->id();
            $table->uuid()->unique();

            // What the operator picks it by, in a dropdown of a dozen.
            $table->string('name');
            // Prefilled into the subject line too; a template that fills only
            // the body leaves half the mail to be typed every time.
            $table->string('subject');
            $table->text('body');
            $table->unsignedSmallInteger('sort')->default(0);
            // Retired rather than deleted: an answer that is no longer given is
            // still the answer somebody was given last year.
            $table->boolean('active')->default(true);
            $table->timestamps();

            $table->index(['active', 'sort']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('mail_templates');
    }
};
