<?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::create('survey_recipients',function(Blueprint $t){
            $t->id();$t->foreignId('survey_id')->index();$t->morphs('respondent');
            $t->uuid('token')->unique();$t->string('email')->nullable();$t->string('status')->default('pending');
            $t->dateTime('sent_at')->nullable();$t->dateTime('opened_at')->nullable();$t->dateTime('responded_at')->nullable();
            $t->text('delivery_error')->nullable();$t->timestamps();
            $t->unique(['survey_id','respondent_type','respondent_id'],'survey_recipient_unique');
        });
        Schema::table('survey_responses',function(Blueprint $t){
            $t->foreignId('survey_recipient_id')->nullable()->index();
            $t->string('ip_address',45)->nullable();
        });
    }
    public function down(): void {
        Schema::table('survey_responses',fn(Blueprint $t)=>$t->dropColumn(['survey_recipient_id','ip_address']));
        Schema::dropIfExists('survey_recipients');
    }
};
