<?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
    {
        if (!Schema::hasTable('researcher_teams')) {
            Schema::create('researcher_teams', function (Blueprint $table) {
                $table->id();
                $table->foreignId('researcher_id')->constrained('researcher_profiles')->onDelete('cascade');
                $table->foreignId('user_id')->constrained('users')->onDelete('cascade');
                $table->string('role')->default('member'); // member, lead, admin, etc.
                $table->timestamp('added_at')->useCurrent();
                $table->timestamps();
                
                $table->unique(['researcher_id', 'user_id']);
                $table->index(['researcher_id', 'role']);
            });
        }
    }

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