<?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('md_instansi', function (Blueprint $table) {
            $table->id();
            $table->string('nama', 150);
            $table->text('deskripsi')->nullable();
            $table->integer('kuota')->default(0);
            $table->timestamps();
        });

        Schema::create('md_bidang', function (Blueprint $table) {
            $table->id();
            $table->string('nama', 100);
            $table->timestamps();
        });

        Schema::create('md_pembimbing_magang', function (Blueprint $table) {
            $table->id();
            $table->string('nama', 150);
            $table->foreignId('bidang_id')->nullable()->constrained('md_bidang')->nullOnDelete();
            $table->timestamps();
        });

        // Relasi pivot antara pembimbing dan peserta magang
        Schema::create('md_peserta_pembimbing', function (Blueprint $table) {
            $table->id();
            $table->foreignId('pembimbing_id')->constrained('md_pembimbing_magang')->onDelete('cascade');
            $table->foreignId('peserta_id')->constrained('md_user')->onDelete('cascade');
            $table->timestamps();
            
            $table->unique(['pembimbing_id', 'peserta_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('md_peserta_pembimbing');
        Schema::dropIfExists('md_pembimbing_magang');
        Schema::dropIfExists('md_bidang');
        Schema::dropIfExists('md_instansi');
    }
};
