<?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('notification_templates', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('title');
            $table->string('slug', 100);
            $table->string('type', 40)->index();
            $table->string('channel', 30)->index();
            $table->string('subject')->nullable();
            $table->longText('message');
            $table->json('variables')->nullable();
            $table->string('language', 12)->default('en');
            $table->string('status', 30)->default('active')->index();
            $table->json('channel_options')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['slug', 'channel', 'language', 'deleted_at'], 'notification_template_identity_unique');
        });

        Schema::create('notifications', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('device_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('notification_template_id')->nullable()->constrained()->nullOnDelete();
            $table->string('title');
            $table->longText('message');
            $table->string('channel', 30)->index();
            $table->string('priority', 20)->default('normal')->index();
            $table->string('category', 40)->index();
            $table->string('status', 30)->default('queued')->index();
            $table->json('data')->nullable();
            $table->string('deep_link')->nullable();
            $table->string('image_url')->nullable();
            $table->timestamp('scheduled_at')->nullable()->index();
            $table->timestamp('sent_at')->nullable()->index();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamp('read_at')->nullable()->index();
            $table->timestamp('archived_at')->nullable()->index();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'read_at', 'created_at']);
            $table->index(['device_id', 'created_at']);
            $table->index(['category', 'priority', 'status']);
        });

        Schema::create('notification_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('notification_id')->constrained()->cascadeOnDelete();
            $table->string('provider', 40)->index();
            $table->unsignedSmallInteger('attempt')->default(1);
            $table->longText('request_payload')->nullable();
            $table->longText('response_payload')->nullable();
            $table->string('response_code', 30)->nullable();
            $table->string('status', 30)->index();
            $table->unsignedInteger('response_time_ms')->nullable();
            $table->text('error_message')->nullable();
            $table->timestamp('created_at')->useCurrent()->index();

            $table->index(['notification_id', 'created_at']);
            $table->index(['provider', 'status', 'created_at']);
        });

        Schema::create('push_devices', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('device_id')->nullable()->constrained()->cascadeOnDelete();
            $table->string('platform', 20)->index();
            $table->text('device_token');
            $table->string('device_token_hash', 64)->unique();
            $table->string('app_version', 30)->nullable();
            $table->string('os_version', 30)->nullable();
            $table->string('locale', 12)->nullable();
            $table->string('timezone', 64)->nullable();
            $table->timestamp('last_active')->nullable()->index();
            $table->string('status', 30)->default('active')->index();
            $table->timestamp('token_refreshed_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'status']);
            $table->index(['device_id', 'status']);
        });

        Schema::create('sms_logs', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('notification_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->text('mobile');
            $table->string('mobile_hash', 64)->index();
            $table->string('provider', 40)->index();
            $table->longText('message');
            $table->string('status', 30)->index();
            $table->string('message_id')->nullable()->index();
            $table->decimal('cost', 12, 4)->nullable();
            $table->string('currency', 3)->nullable();
            $table->unsignedSmallInteger('attempts')->default(0);
            $table->timestamp('sent_at')->nullable()->index();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['user_id', 'created_at']);
            $table->index(['provider', 'status', 'created_at']);
        });

        Schema::create('email_logs', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('notification_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->text('email');
            $table->string('email_hash', 64)->index();
            $table->string('subject');
            $table->string('provider', 40)->index();
            $table->string('status', 30)->index();
            $table->string('message_id')->nullable()->index();
            $table->boolean('opened')->default(false)->index();
            $table->boolean('clicked')->default(false)->index();
            $table->unsignedInteger('open_count')->default(0);
            $table->unsignedInteger('click_count')->default(0);
            $table->unsignedSmallInteger('attempts')->default(0);
            $table->timestamp('sent_at')->nullable()->index();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamp('opened_at')->nullable();
            $table->timestamp('clicked_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['user_id', 'created_at']);
            $table->index(['provider', 'status', 'created_at']);
        });

        Schema::create('push_logs', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('notification_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('push_device_id')->nullable()->constrained()->nullOnDelete();
            $table->string('provider', 40)->default('firebase');
            $table->string('message_id')->nullable()->index();
            $table->string('status', 30)->index();
            $table->unsignedSmallInteger('attempts')->default(0);
            $table->timestamp('sent_at')->nullable()->index();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['provider', 'status', 'created_at']);
        });

        Schema::create('scheduled_notifications', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('notification_template_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('admin_id')->nullable()->constrained()->nullOnDelete();
            $table->string('name');
            $table->string('frequency', 30)->default('once');
            $table->string('cron_expression')->nullable();
            $table->string('timezone', 64)->default('UTC');
            $table->json('audience');
            $table->json('channels');
            $table->json('payload');
            $table->timestamp('next_run_at')->index();
            $table->timestamp('last_run_at')->nullable();
            $table->timestamp('ends_at')->nullable();
            $table->string('status', 30)->default('active')->index();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['status', 'next_run_at']);
        });

        Schema::create('notification_preferences', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
            $table->boolean('push_enabled')->default(true);
            $table->boolean('sms_enabled')->default(true);
            $table->boolean('email_enabled')->default(true);
            $table->boolean('whatsapp_enabled')->default(false);
            $table->boolean('telegram_enabled')->default(false);
            $table->json('muted_categories')->nullable();
            $table->json('channel_overrides')->nullable();
            $table->time('quiet_hours_start')->nullable();
            $table->time('quiet_hours_end')->nullable();
            $table->string('timezone', 64)->default('UTC');
            $table->string('language', 12)->default('en');
            $table->timestamps();
        });

        Schema::create('notification_webhooks', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('name');
            $table->text('url');
            $table->string('url_hash', 64)->unique();
            $table->text('authentication_headers')->nullable();
            $table->json('subscribed_categories')->nullable();
            $table->boolean('verify_tls')->default(true);
            $table->boolean('is_active')->default(true)->index();
            $table->unsignedSmallInteger('max_attempts')->default(3);
            $table->unsignedInteger('timeout_seconds')->default(10);
            $table->timestamps();
        });

        Schema::create('webhook_delivery_logs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('notification_webhook_id')->constrained()->cascadeOnDelete();
            $table->foreignId('notification_id')->nullable()->constrained()->nullOnDelete();
            $table->unsignedSmallInteger('attempt')->default(1);
            $table->longText('request_payload');
            $table->longText('response_payload')->nullable();
            $table->unsignedSmallInteger('response_code')->nullable();
            $table->string('status', 30)->index();
            $table->unsignedInteger('response_time_ms')->nullable();
            $table->timestamp('next_retry_at')->nullable()->index();
            $table->timestamp('created_at')->useCurrent();

            $table->index(['notification_webhook_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('webhook_delivery_logs');
        Schema::dropIfExists('notification_webhooks');
        Schema::dropIfExists('notification_preferences');
        Schema::dropIfExists('scheduled_notifications');
        Schema::dropIfExists('push_logs');
        Schema::dropIfExists('email_logs');
        Schema::dropIfExists('sms_logs');
        Schema::dropIfExists('push_devices');
        Schema::dropIfExists('notification_logs');
        Schema::dropIfExists('notifications');
        Schema::dropIfExists('notification_templates');
    }
};
