<?php

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

class CreateClcReminderLogsTable extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('clc_reminder_logs')) {
            Schema::create('clc_reminder_logs', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id');
                $table->unsignedInteger('vaccination_id');
                $table->unsignedInteger('contact_id');
                $table->unsignedInteger('patient_id');
                $table->enum('channel', ['email', 'wa'])->default('email');
                $table->enum('template_type', ['email_due', 'email_overdue', 'wa_due', 'wa_overdue']);
                $table->string('sent_to', 255)->nullable()->comment('Email or phone number used');
                $table->enum('status', ['sent', 'failed'])->default('sent');
                $table->text('error_message')->nullable();
                $table->timestamp('sent_at')->nullable();
                $table->timestamps();

                $table->index(['vaccination_id', 'channel', 'template_type']);
                $table->index(['business_id', 'sent_at']);
            });
        }
    }

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