<?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('project_contributions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('project_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('financial_assignment_id')->nullable()->constrained('financial_assignments')->nullOnDelete();
            $table->decimal('amount', 10, 2);
            $table->string('status', 20)->default('pending'); // pending, completed
            $table->timestamp('contributed_at')->nullable();
            $table->timestamps();

            // Prevent duplicate contribution records for the same assignment
            $table->unique(['project_id', 'user_id', 'financial_assignment_id'], 'project_user_assignment_unique');
            $table->index(['project_id', 'status']);
            $table->index(['user_id', 'project_id']);
        });
    }

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