<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — Question Bank — Per-User Ownership
//
//  What this migration does:
//    Adds user_id to both question bank tables so that:
//
//    question_bank_sections:
//      user_id = null  → platform section (seeded: Finance, Marketing,
//                        Sales, General) — visible to all members
//      user_id = X     → personal section created by member X
//                        — visible only to that member
//
//    question_bank_items:
//      user_id = null  → platform question (seeded) — visible to all
//      user_id = X     → personal question saved by member X
//                        — visible only to that member
//
//  This matches the original design: every member has their own
//  personal question bank alongside the platform bank.
// ══════════════════════════════════════════════════════════════════

return new class extends Migration
{
    public function up(): void
    {
        // ── question_bank_sections ─────────────────────────────
        Schema::table('question_bank_sections', function (Blueprint $table) {
            $table->foreignId('user_id')
                  ->nullable()
                  ->after('id')
                  ->constrained('users')
                  ->cascadeOnDelete();
        });

        // ── question_bank_items ────────────────────────────────
        Schema::table('question_bank_items', function (Blueprint $table) {
            $table->foreignId('user_id')
                  ->nullable()
                  ->after('id')
                  ->constrained('users')
                  ->cascadeOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('question_bank_items', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
            $table->dropColumn('user_id');
        });

        Schema::table('question_bank_sections', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
            $table->dropColumn('user_id');
        });
    }
};
