<?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::create('popups', function (Blueprint $table) {
            $table->id();

            // 🔴 필수 컬럼 (조인/인덱싱)
            $table->foreignId('tenant_id')->constrained()->cascadeOnDelete();
            $table->string('target_type', 20)->default('all')->comment('대상 유형: all, department');
            $table->unsignedBigInteger('target_id')->nullable()->comment('대상 ID (부서 ID)');
            $table->string('title', 200)->comment('제목');
            $table->text('content')->comment('내용');
            $table->string('status', 20)->default('inactive')->index()->comment('상태: active, inactive');
            $table->dateTime('started_at')->nullable()->index()->comment('노출 시작일');
            $table->dateTime('ended_at')->nullable()->index()->comment('노출 종료일');

            // 🟢 가변 컬럼 (JSON)
            $table->json('options')->nullable()->comment('확장 옵션');

            // 감사 컬럼
            $table->unsignedBigInteger('created_by')->nullable()->comment('작성자');
            $table->unsignedBigInteger('updated_by')->nullable()->comment('수정자');
            $table->unsignedBigInteger('deleted_by')->nullable()->comment('삭제자');

            $table->timestamps();
            $table->softDeletes();

            // 인덱스
            $table->index('target_type');
            $table->index(['tenant_id', 'status', 'started_at', 'ended_at']);
        });
    }

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