<?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::create('srs_reviews', function (Blueprint $table) {
            $table->uuid('id')->primary();
            
            $table->foreignUuid('user_id')->constrained()->cascadeOnDelete();
            $table->foreignUuid('vocabulary_id')->constrained('vocabularies')->cascadeOnDelete();
            
            // SM-2 Algorithm Fields
            $table->decimal('ease_factor', 5, 2)->default(2.50); // Minimum usually 1.3
            $table->integer('interval')->default(0); // Days
            $table->integer('repetitions')->default(0); // Consecutive success count
            
            $table->timestamp('next_review_at'); // When it becomes due
            $table->timestamp('last_review_at')->nullable();
            
            $table->string('status')->default('new'); // new, learning, review, graduated
            
            $table->timestamps();
            
            // Indexes for performance (querying 'due' cards is frequent)
            $table->index(['user_id', 'next_review_at']);
            // Uniqueness: One review entry per user per vocab? Or usage log?
            // Usually SRS keeps ONE state per item. A separate "review_logs" table would track history.
            // For now, this table tracks CURRENT state.
            $table->unique(['user_id', 'vocabulary_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('srs_reviews');
    }
};
