<?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('terminal_knowledge_bases', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('terminal_id')->comment('终端ID');
            $table->unsignedBigInteger('knowledge_base_id')->comment('知识库ID');
            $table->integer('priority')->default(0)->comment('优先级');
            $table->timestamps();

            // 添加外键约束
            $table->foreign('terminal_id')
                  ->references('id')
                  ->on('terminals')
                  ->onDelete('cascade');

            // 添加唯一索引，确保同一终端不会重复关联同一知识库
            $table->unique(['terminal_id', 'knowledge_base_id'], 'uk_terminal_kb');
        });
    }

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