<?php

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::table('video_views', function (Blueprint $table) {
            $table->string('ip_address', 45)->nullable()->after('video_id');
            $table->string('country', 2)->nullable()->after('ip_address');
            $table->string('country_name', 100)->nullable()->after('country');
            // Allow null so guest (unauthenticated) views can be recorded
            $table->unsignedBigInteger('user_id')->nullable()->change();
        });
    }

    public function down(): void
    {
        Schema::table('video_views', function (Blueprint $table) {
            $table->dropColumn(['ip_address', 'country', 'country_name']);
            $table->unsignedBigInteger('user_id')->nullable(false)->change();
        });
    }
};
