<?php

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

class CreateMsgraphModelChangesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('msgraph_model_changes', function (Blueprint $table) {
            $table->id();

            // Polymorphic relationship to the model (Committee or WorkGroup)
            $table->morphs('model'); // creates model_type and model_id columns

            // Action type: model_created, model_updated, model_deleted, status_changed, title_changed
            $table->enum('action', [
                'model_created',
                'model_updated',
                'model_deleted',
                'status_changed',
                'title_changed'
            ]);

            // Status changes
            $table->string('old_status', 20)->nullable(); // previous status
            $table->string('new_status', 20)->nullable(); // new status

            // Title changes
            $table->string('old_title')->nullable(); // previous title
            $table->string('new_title')->nullable(); // new title

            // Language for title changes (since titles are in language tables)
            $table->string('language', 5)->nullable(); // language code (e.g., 'en', 'es')

            // Additional metadata as JSON
            $table->json('metadata')->nullable();

            // Processing status
            $table->boolean('processed')->default(false);
            $table->timestamp('processed_at')->nullable();

            // Priority for processing order
            $table->enum('priority', ['low', 'normal', 'high'])->default('normal');

            $table->timestamps();

            // Indexes for better performance
            $table->index(['processed', 'priority', 'created_at']);
            $table->index(['action', 'created_at']);
        });
    }

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