<?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
    {
        Schema::table('today_issues', function (Blueprint $table) {
            $table->unsignedBigInteger('target_user_id')
                ->nullable()
                ->after('source_id')
                ->comment('특정 대상 사용자 ID (null이면 테넌트 전체)');

            $table->foreign('target_user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            $table->index(['tenant_id', 'target_user_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('today_issues', function (Blueprint $table) {
            $table->dropForeign(['target_user_id']);
            $table->dropIndex(['tenant_id', 'target_user_id']);
            $table->dropColumn('target_user_id');
        });
    }
};
