<?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('playlist_videos', function (Blueprint $table) {
            $table->id();
            $table->foreignId('playlist_id')->constrained()->onDelete('cascade');
            $table->foreignId('video_id')->constrained()->onDelete('cascade');
            $table->integer('position')->default(0); // For ordering videos in playlist
            $table->timestamp('added_at')->useCurrent();
            // Watch progress - remember where user left off
            $table->integer('watched_seconds')->default(0);
            $table->boolean('watched')->default(false);
            $table->timestamp('last_watched_at')->nullable();
            $table->timestamps();

            $table->unique(['playlist_id', 'video_id']);
            $table->index(['playlist_id', 'position']);
            $table->index(['video_id', 'last_watched_at']);
        });
    }

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