<?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('progress_snapshots', function (Blueprint $table) {
            $table->id();
            $table->morphs('trackable'); // Phase, Feature, Task
            $table->date('snapshot_date');
            $table->decimal('progress', 5, 2)->default(0); // 0-100
            $table->date('planned_start')->nullable();
            $table->date('planned_end')->nullable();
            $table->date('actual_start')->nullable();
            $table->date('actual_end')->nullable();
            $table->string('method')->default('manual'); // manual|inspection|task|geometry|hybrid
            $table->json('metadata')->nullable(); // {earned_value, planned_value, actual_cost, spi, cpi, ...}
            $table->timestamps();

            // Short index name for MySQL 64-char limit
            $table->unique(['trackable_type', 'trackable_id', 'snapshot_date'], 'ps_trackable_date_unique');
            $table->index(['trackable_type', 'trackable_id', 'snapshot_date'], 'ps_trackable_date_idx');
            $table->index(['snapshot_date'], 'ps_snapshot_date_idx');
        });
    }

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