<?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('video_downloads', function (Blueprint $table) {
            $table->id();
            $table->foreignId('video_id')->constrained()->onDelete('cascade');
            $table->foreignId('user_id')->nullable()->constrained()->onDelete('set null');
            $table->string('ip_address', 45)->nullable();
            $table->string('country', 2)->nullable();
            $table->string('country_name', 100)->nullable();
            $table->string('type', 10)->default('video'); // 'video' or 'mp3'
            $table->timestamp('downloaded_at')->useCurrent();

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

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