<?php

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

/**
 * 敏感词库迁移
 *
 * 依据法规：
 * - 《网络安全法》第十二条、第四十七条
 * - 《网络信息内容生态治理规定》第六条、第九条
 * - TC260-003 内容安全标准（31 类风险分类）
 */
return new class extends Migration
{
    public function up(): void
    {
        // 敏感词分类表
        Schema::create('ks_word_categories', function (Blueprint $table) {
            $table->id();
            $table->string('code', 32)->unique()->comment('分类编码: political/pornographic/violent...');
            $table->string('name', 64)->comment('分类名称');
            $table->string('risk_level', 10)->default('medium')->comment('风险等级: high/medium/low');
            $table->boolean('status')->default(true)->comment('状态: 1=启用 0=禁用');
            $table->integer('sort')->default(0)->comment('排序');
            $table->timestamps();

            $table->index(['status', 'sort']);
            $table->index('risk_level');
        });

        // 敏感词表
        Schema::create('ks_sensitive_words', function (Blueprint $table) {
            $table->id();
            $table->string('word', 64)->comment('敏感词');
            $table->string('category', 32)->index()->comment('分类编码: political/pornographic/violent/gambling/advertising/insult...');
            $table->string('risk_level', 10)->default('medium')->comment('风险等级: high/medium/low');
            $table->tinyInteger('status')->default(1)->comment('1=启用 0=禁用');
            $table->unsignedBigInteger('variant_of')->nullable()->comment('变体所属原词 ID（NULL=原词）');
            $table->string('variant_type', 20)->nullable()->comment('变体类型: homophone/char_split/pinyin/abbreviation');
            $table->string('version', 20)->default('1.0.0')->comment('词库版本号');
            $table->string('source', 50)->nullable()->comment('词库来源: manual/sync/import');
            $table->timestamps();

            // 同一词在同一分类下唯一
            $table->unique(['word', 'category']);
            $table->index(['status', 'risk_level']);
            $table->index('variant_of');
            $table->index('version');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('ks_sensitive_words');
        Schema::dropIfExists('ks_word_categories');
    }
};
