<?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('watch_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
            // content_type: video | tv | radio | slider
            $table->enum('content_type', ['video', 'tv', 'radio', 'slider'])->default('video');
            $table->unsignedBigInteger('content_id');
            $table->unsignedInteger('watched_seconds')->default(0);
            $table->unsignedInteger('total_seconds')->default(0);
            $table->boolean('completed')->default(false);
            $table->timestamp('watched_at')->useCurrent();
            $table->timestamps();

            // Only one history entry per user+content combo
            $table->unique(['user_id', 'content_type', 'content_id']);
            $table->index(['user_id', 'watched_at']);
        });
    }

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