<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Adds unique constraints to timer and progress tables to prevent duplicates:
     * - One timer record per (enrollment, topic) pair
     * - One progress record per (enrollment, subtopic) pair
     *
     * Requirements: 12.2, 12.3
     */
    public function up(): void
    {
        Schema::table('self_paced_topic_timers', function (Blueprint $table) {
            $table->unique(
                ['enrollment_id', 'topic_id'],
                'uq_sp_timer_enrollment_topic'
            );
        });

        Schema::table('self_paced_learning_progress', function (Blueprint $table) {
            $table->unique(
                ['enrollment_id', 'subtopic_id'],
                'uq_sp_progress_enrollment_subtopic'
            );
        });
    }

    public function down(): void
    {
        Schema::table('self_paced_topic_timers', function (Blueprint $table) {
            $table->dropUnique('uq_sp_timer_enrollment_topic');
        });

        Schema::table('self_paced_learning_progress', function (Blueprint $table) {
            $table->dropUnique('uq_sp_progress_enrollment_subtopic');
        });
    }
};
