<?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('download_logs', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('user_id')->unsigned();
            $table->bigInteger('media_id')->unsigned()->nullable(); // null for zip downloads
            $table->bigInteger('product_file_id')->unsigned();
            $table->bigInteger('order_id')->unsigned();
            $table->dateTime('downloaded_at');
            $table->timestamps();

            $table->index(['user_id', 'downloaded_at']);
            $table->index(['order_id', 'downloaded_at']);

            $table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
            $table->foreign('order_id')->references('id')->on('orders')->cascadeOnDelete();
        });
    }

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