<?php

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

class CreateWhatsappCampaignRecipientsTable extends Migration
{
    public function up()
    {
        Schema::create('whatsapp_campaign_recipients', function (Blueprint $table) {
            $table->increments('id');

            $table->unsignedInteger('campaign_id');
            $table->unsignedInteger('person_id')->nullable(); // العميل
            $table->string('to'); // رقم الجوال بعد التوحيد

            // 1=PENDING, 2=SENT, 3=FAILED, 4=DELIVERED
            $table->unsignedTinyInteger('status_code')->default(1);
            $table->string('status_label')->nullable();

            $table->unsignedTinyInteger('attempts')->default(0);
            $table->dateTime('next_attempt_at')->nullable(); // retryDelay
            $table->dateTime('sent_at')->nullable();
            $table->dateTime('delivered_at')->nullable();

            $table->string('api_msg_id')->nullable(); // msgId من Wasender
            $table->text('error')->nullable();

            $table->timestamps();

            $table->index(['campaign_id', 'status_code']);
            $table->index(['campaign_id', 'person_id']);
            $table->index(['api_msg_id']);
        });
    }

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