<?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
    {
        if (Schema::hasTable('user_follow_analytics')) {
            return;
        }

        Schema::create('user_follow_analytics', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->date('date');
            $table->unsignedInteger('followers_gained')->default(0);
            $table->unsignedInteger('followers_lost')->default(0);
            $table->unsignedInteger('follows_made')->default(0);
            $table->unsignedInteger('unfollows_made')->default(0);
            $table->timestamps();

            $table->unique(['user_id', 'date']);
            $table->index(['date', 'user_id']);
        });
    }

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