<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('surveys', function (Blueprint $table) {
            $table->boolean('allow_anonymous')->default(false)->after('target_audience');
            $table->uuid('anonymous_token')->nullable()->unique()->after('allow_anonymous');
        });
        Schema::table('survey_recipients', function (Blueprint $table) {
            $table->unsignedInteger('delivery_attempts')->default(0)->after('status');
            $table->unsignedInteger('reminder_count')->default(0)->after('delivery_attempts');
            $table->dateTime('last_reminded_at')->nullable()->after('sent_at');
        });
        Schema::table('survey_responses', function (Blueprint $table) {
            $table->unique('survey_recipient_id', 'survey_response_recipient_unique');
        });
    }

    public function down(): void
    {
        Schema::table('survey_responses', fn (Blueprint $table) => $table->dropUnique('survey_response_recipient_unique'));
        Schema::table('survey_recipients', fn (Blueprint $table) => $table->dropColumn(['delivery_attempts','reminder_count','last_reminded_at']));
        Schema::table('surveys', fn (Blueprint $table) => $table->dropColumn(['allow_anonymous','anonymous_token']));
    }
};
