<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('ai_guard_logs', function (Blueprint $table): void {
            $table->bigIncrements('id');
            $table->string('emoji_slug', 64);
            $table->string('lang', 8);
            $table->string('region', 8)->nullable();
            $table->string('keyword', 128);
            $table->string('decision', 16);
            $table->string('reason', 32);
            $table->string('detected_lang', 8)->nullable();
            $table->string('detail_en', 255)->nullable();
            $table->string('detail_local', 255)->nullable();
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('ai_judgments', function (Blueprint $table): void {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('keyword_id')->nullable();
            $table->string('emoji_slug', 190);
            $table->string('lang', 16);
            $table->string('keyword', 64);
            $table->string('model', 64)->default('gemini-free');
            $table->string('verdict', 16);
            $table->decimal('confidence', 5, 4)->nullable();
            $table->longText('raw_response')->nullable();
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('ai_lang_cache', function (Blueprint $table): void {
            $table->string('keyword', 128)->primary();
            $table->string('detected_lang', 8);
            $table->timestamp('last_seen')->nullable();
        });

        Schema::create('ai_provider_usage', function (Blueprint $table): void {
            $table->string('provider', 96);
            $table->string('period', 7);
            $table->unsignedBigInteger('tokens_used')->default(0);
            $table->primary(['provider', 'period']);
        });

        Schema::create('contributor_rewards', function (Blueprint $table): void {
            $table->bigIncrements('id');
            $table->string('user_id', 64);
            $table->string('type', 32);
            $table->string('reason', 128);
            $table->string('issued_by', 64)->default('system');
            $table->timestamp('issued_at')->nullable();
            $table->timestamp('redeemed_at')->nullable();
            $table->longText('meta')->nullable();
        });

        Schema::create('email_queue', function (Blueprint $table): void {
            $table->bigIncrements('id');
            $table->string('user_id', 64)->nullable();
            $table->string('to_email', 255);
            $table->string('template', 64);
            $table->longText('payload')->nullable();
            $table->string('provider', 32)->default('elastic_email');
            $table->string('status', 32)->default('queued');
            $table->string('last_error', 255)->nullable();
            $table->timestamp('created_at')->nullable();
            $table->timestamp('sent_at')->nullable();
        });

        Schema::create('emojis', function (Blueprint $table): void {
            $table->unsignedInteger('emoji_id')->primary();
            $table->string('slug', 191);
            $table->string('emoji_char', 64);
            $table->string('name', 191);
            $table->string('category', 128);
            $table->string('subcategory', 128);
            $table->string('unified', 128);
            $table->string('default_presentation', 16);
            $table->string('version', 16);
            $table->boolean('supports_skin_tone')->default(false);
            $table->string('permalink', 255);
            $table->text('description')->nullable();
            $table->string('meta_title', 255)->nullable();
            $table->string('meta_description', 255)->nullable();
            $table->string('title', 255)->nullable();
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();
        });

        Schema::create('emoji_aliases', function (Blueprint $table): void {
            $table->unsignedInteger('emoji_id');
            $table->string('alias', 191);
            $table->unique(['emoji_id', 'alias']);
        });

        Schema::create('emoji_keywords', function (Blueprint $table): void {
            $table->bigIncrements('id');
            $table->string('emoji_slug', 190);
            $table->string('lang', 2);
            $table->string('region', 2)->default('');
            $table->string('keyword', 64);
            $table->string('status', 16)->default('private');
            $table->string('owner_user_id', 64)->nullable();
            $table->string('visibility', 32)->default('private');
            $table->string('public_key', 900)->nullable();
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();
        });

        Schema::create('emoji_shortcodes', function (Blueprint $table): void {
            $table->unsignedInteger('emoji_id');
            $table->string('shortcode', 191);
            $table->string('kind', 16)->default('primary');
            $table->unique(['emoji_id', 'shortcode', 'kind']);
        });

        Schema::create('keyword_votes', function (Blueprint $table): void {
            $table->unsignedBigInteger('keyword_id');
            $table->string('user_id', 64);
            $table->string('vote', 8);
            $table->tinyInteger('value');
            $table->timestamp('created_at')->nullable();
            $table->primary(['keyword_id', 'user_id']);
        });

        Schema::create('license_bindings', function (Blueprint $table): void {
            $table->string('license_key', 64);
            $table->string('user_id', 64);
            $table->timestamp('created_at')->nullable();
            $table->unique(['license_key', 'user_id']);
        });

        Schema::create('magic_links', function (Blueprint $table): void {
            $table->string('token_hash', 64)->primary();
            $table->string('user_id', 64);
            $table->string('purpose', 32)->default('login');
            $table->timestamp('expires_at');
            $table->timestamp('used_at')->nullable();
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('moderation_events', function (Blueprint $table): void {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('keyword_id');
            $table->string('old_status', 16)->nullable();
            $table->string('new_status', 16);
            $table->string('reason', 255)->nullable();
            $table->string('actor', 64)->default('system');
            $table->timestamp('created_at')->nullable();
        });

        Schema::create('user_prefs', function (Blueprint $table): void {
            $table->string('user_id', 64)->primary();
            $table->longText('preferred_languages');
            $table->timestamp('updated_at')->nullable();
        });

        Schema::create('legacy_users', function (Blueprint $table): void {
            $table->string('user_id', 64)->primary();
            $table->string('email', 255)->nullable();
            $table->string('username', 32);
            $table->boolean('opted_in')->default(false);
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();
        });

        Schema::create('legacy_sessions', function (Blueprint $table): void {
            $table->string('session_id', 64)->primary();
            $table->string('user_id', 64);
            $table->string('ip_hash', 64)->nullable();
            $table->string('ua_hash', 64)->nullable();
            $table->timestamp('expires_at');
            $table->timestamp('created_at')->nullable();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('legacy_sessions');
        Schema::dropIfExists('legacy_users');
        Schema::dropIfExists('user_prefs');
        Schema::dropIfExists('moderation_events');
        Schema::dropIfExists('magic_links');
        Schema::dropIfExists('license_bindings');
        Schema::dropIfExists('keyword_votes');
        Schema::dropIfExists('emoji_shortcodes');
        Schema::dropIfExists('emoji_keywords');
        Schema::dropIfExists('emoji_aliases');
        Schema::dropIfExists('emojis');
        Schema::dropIfExists('email_queue');
        Schema::dropIfExists('contributor_rewards');
        Schema::dropIfExists('ai_provider_usage');
        Schema::dropIfExists('ai_lang_cache');
        Schema::dropIfExists('ai_judgments');
        Schema::dropIfExists('ai_guard_logs');
    }
};
