<?php

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

/**
 * A customer's support requests.
 *
 * The page had a button that raised a toast saying the form was "only hinted
 * at in the prototype", three invented ticket titles in the translation file,
 * and no way to see what you had asked. It looked thin because there was
 * nothing on it.
 *
 * The record exists so that both sides can see the same thing: the customer
 * gets a history instead of a sent e-mail they cannot check on, and the
 * console gets the request with the instance already attached, rather than
 * asking the customer which server they mean.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('support_requests', function (Blueprint $table) {
            $table->id();
            $table->uuid()->unique();
            $table->foreignId('customer_id')->constrained()->cascadeOnDelete();

            // Whose cloud this is about. Nullable: a question can arrive before
            // provisioning finishes, and losing the instance later must not
            // take the conversation with it.
            $table->foreignId('instance_id')->nullable()->constrained()->nullOnDelete();

            $table->string('subject');
            $table->string('category', 32)->default('other');
            $table->text('body');

            // open → in_progress → answered/closed. Kept as a string rather
            // than an enum so a workflow change is not a migration.
            $table->string('status', 24)->default('open');

            // Who wrote it — the seat, not the account. On a shared login the
            // account name says nothing about who is actually asking.
            $table->string('reported_by')->nullable();

            $table->timestamp('answered_at')->nullable();
            $table->timestamps();

            // The customer's own list, newest first — the only query the panel
            // makes.
            $table->index(['customer_id', 'created_at']);
            // The console's queue: everything still waiting, oldest first.
            $table->index(['status', 'created_at']);
        });
    }

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