<?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('whatsapp_messages', function (Blueprint $t) {
            $t->id(); $t->foreignId('lead_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('user_id')->nullable()->constrained()->nullOnDelete(); $t->string('to_phone');
            $t->string('direction')->default('outbound'); $t->string('type')->default('text');
            $t->string('status')->default('queued'); $t->string('provider_message_id')->nullable()->index();
            $t->text('body')->nullable(); $t->json('payload')->nullable(); $t->text('error_message')->nullable(); $t->timestamps();
        });
        Schema::create('notification_logs', function (Blueprint $t) {
            $t->id(); $t->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $t->foreignId('lead_id')->nullable()->constrained()->nullOnDelete(); $t->string('channel')->default('in_app');
            $t->string('type'); $t->string('title'); $t->text('message'); $t->timestamp('read_at')->nullable(); $t->timestamps();
        });
        Schema::create('notifications', function (Blueprint $t) {
            $t->id(); $t->foreignId('user_id')->constrained()->cascadeOnDelete(); $t->string('title');
            $t->text('message'); $t->string('type')->index(); $t->timestamp('read_at')->nullable(); $t->json('data')->nullable(); $t->timestamps();
            $t->index(['user_id','read_at']);
        });
    }
    public function down(): void { Schema::dropIfExists('notifications'); Schema::dropIfExists('notification_logs'); Schema::dropIfExists('whatsapp_messages'); }
};
