<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use App\Models\Video;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('videos', function (Blueprint $table) {
            $table->string('share_token', 64)->nullable()->unique()->after('share_count');
        });

        // Backfill existing videos
        Video::whereNull('share_token')->each(function ($video) {
            $video->updateQuietly(['share_token' => Str::random(32)]);
        });
    }

    public function down(): void
    {
        Schema::table('videos', function (Blueprint $table) {
            $table->dropColumn('share_token');
        });
    }
};
