<?php

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

class CreateLaravelCrmSmsCampaignRecipientsTable extends Migration
{
    public function up()
    {
        Schema::create(config('laravel-crm.db_table_prefix').'sms_campaign_recipients', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('external_id');
            $table->unsignedBigInteger('team_id')->index()->nullable();
            $table->unsignedBigInteger('sms_campaign_id')->index();
            $table->unsignedBigInteger('phone_id')->index();
            $table->unsignedBigInteger('person_id')->nullable()->index();
            $table->string('tracking_token', 64)->unique();
            $table->enum('status', ['pending', 'sent', 'delivered', 'failed', 'bounced', 'skipped'])->default('pending')->index();
            $table->timestamp('sent_at')->nullable();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamp('first_clicked_at')->nullable();
            $table->timestamp('last_clicked_at')->nullable();
            $table->unsignedInteger('clicks_count')->default(0);
            $table->timestamp('unsubscribed_at')->nullable();
            $table->string('clicksend_message_id')->nullable();
            $table->text('error')->nullable();
            $table->timestamps();

            $table->unique(['sms_campaign_id', 'phone_id'], 'crm_scr_campaign_phone_unique');
        });
    }

    public function down()
    {
        Schema::dropIfExists(config('laravel-crm.db_table_prefix').'sms_campaign_recipients');
    }
}
