<?php

use App\Models\InstagramNotificationType;
use App\Models\InstagramRepost;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('instagram_notifications', function (Blueprint $table) {
            $table->id();

            $table->string('username')->nullable();
            $table->foreignIdFor(InstagramRepost::class)->nullable()
                ->constrained('instagram_reposts')
                ->noActionOnDelete()
                ->cascadeOnUpdate();
            $table->enum('notification_type', array_column(InstagramNotificationType::cases(), 'value'));
            $table->text('message')->nullable();
            $table->boolean('is_read')->default(false);
            $table->boolean('is_processed')->default(false);

            $table->unique(['username', 'instagram_repost_id', 'notification_type', 'message'], 'unique_instagram_notification');

            $table->timestamps();
        });

        Schema::table('instagram_reposts', function (Blueprint $table) {
            $table->string("repost_reel_id")->unique()->nullable()->comment('Reel ID of the reposted content');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('instagram_notifications');

        Schema::table('instagram_reposts', function (Blueprint $table) {
            $table->dropUnique(['repost_reel_id']);
            $table->dropColumn("repost_reel_id");
        });
    }
};
