<?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
    {
        // Bảng danh mục các chủ đề
        Schema::create('topic_categories', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->string('name')->comment('Tên danh mục'); // Category name
            $table->string('slug')->unique()->comment('URL slug của danh mục'); // URL-friendly slug
            $table->text('description')->nullable()->comment('Mô tả danh mục'); // Category description
            $table->string('color', 7)->default('#3B82F6')->comment('Màu sắc đại diện danh mục'); // Category color
            $table->integer('sort_order')->default(0)->comment('Thứ tự sắp xếp'); // Sort order
            $table->boolean('is_active')->default(true)->comment('Trạng thái hoạt động'); // Active status
            $table->timestamps(); // created_at, updated_at
            $table->softDeletes(); // deleted_at for soft delete

            // Indexes
            $table->index(['slug']);
            $table->index(['is_active', 'sort_order']);
        });

        // Bảng tags cho các chủ đề
        Schema::create('topic_tags', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->string('name')->comment('Tên tag'); // Tag name
            $table->string('slug')->unique()->comment('URL slug của tag'); // URL-friendly slug
            $table->text('description')->nullable()->comment('Mô tả tag'); // Tag description
            $table->string('color', 7)->default('#10B981')->comment('Màu sắc đại diện tag'); // Tag color
            $table->integer('usage_count')->default(0)->comment('Số lần được sử dụng'); // Usage count
            $table->timestamps(); // created_at, updated_at
            $table->softDeletes(); // deleted_at for soft delete

            // Indexes
            $table->index(['slug']);
            $table->index(['usage_count']);
        });

        // Bảng topics (chủ đề)
        Schema::create('topics', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->string('title')->comment('Tiêu đề chủ đề'); // Topic title
            $table->string('slug')->unique()->comment('URL slug của chủ đề'); // URL-friendly slug
            $table->text('short_description')->nullable()->comment('Mô tả ngắn'); // Short description
            $table->longText('content')->comment('Nội dung chủ đề'); // Main content
            $table->integer('view_count')->default(0)->comment('Lượt xem'); // View count
            $table->foreignId('user_id')->constrained()->comment('ID tác giả'); // Author ID

            // Trạng thái chủ đề
            $table->enum('status', ['draft', 'published', 'resolved', 'rejected'])
                ->default('draft')
                ->comment('Trạng thái: mới tạo, đã phát hành, đã giải quyết, bị từ chối');

            // Đánh dấu mức độ ưu tiên
            $table->enum('priority', ['normal', 'urgent'])
                ->default('normal')
                ->comment('Mức độ ưu tiên: bình thường, khẩn cấp');

            // Chế độ comment
            $table->enum('comment_permission', ['all', 'experts_only', 'users_only', 'closed'])
                ->default('all')
                ->comment('Quyền comment: tất cả, chỉ chuyên gia, chỉ người dùng, đóng comment');

            $table->boolean('is_featured')->default(false)->comment('Đánh dấu nổi bật'); // Featured flag
            $table->boolean('is_pinned')->default(false)->comment('Đánh dấu ghim'); // Pinned flag
            $table->integer('like_count')->default(0)->comment('Số lượt thích'); // Like count
            $table->integer('comment_count')->default(0)->comment('Số bình luận'); // Comment count
            $table->timestamp('last_activity_at')->nullable()->comment('Thời gian hoạt động cuối'); // Last activity
            $table->timestamps(); // created_at, updated_at
            $table->softDeletes(); // deleted_at for soft delete

            // Indexes
            $table->index(['slug']);
            $table->index(['user_id']);
            $table->index(['status', 'is_featured', 'is_pinned']);
            $table->index(['priority']);
            $table->index(['view_count']);
            $table->index(['last_activity_at']);
            $table->index(['created_at']);
        });

        // Bảng liên kết topic với category
        Schema::create('topic_category_details', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->foreignId('topic_id')->constrained('topics')->onDelete('cascade')->comment('ID chủ đề'); // Topic ID
            $table->foreignId('topic_category_id')->constrained('topic_categories')->onDelete('cascade')->comment('ID danh mục'); // Category ID
            $table->timestamps(); // created_at, updated_at

            // Unique constraint để tránh trùng lặp
            $table->unique(['topic_id', 'topic_category_id']);

            // Indexes
            $table->index(['topic_id']);
            $table->index(['topic_category_id']);
        });

        // Bảng liên kết topic với tag
        Schema::create('topic_tag_details', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->foreignId('topic_id')->constrained('topics')->onDelete('cascade')->comment('ID chủ đề'); // Topic ID
            $table->foreignId('topic_tag_id')->constrained('topic_tags')->onDelete('cascade')->comment('ID tag'); // Tag ID
            $table->timestamps(); // created_at, updated_at

            // Unique constraint để tránh trùng lặp
            $table->unique(['topic_id', 'topic_tag_id']);

            // Indexes
            $table->index(['topic_id']);
            $table->index(['topic_tag_id']);
        });

        // Bảng comments (bình luận)
        Schema::create('topic_comments', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->foreignId('topic_id')->constrained('topics')->onDelete('cascade')->comment('ID chủ đề'); // Topic ID
            $table->foreignId('user_id')->constrained()->comment('ID người bình luận'); // Commenter ID
            $table->longText('content')->comment('Nội dung bình luận'); // Comment content
            $table->unsignedBigInteger('parent_id')->default(0)->comment('ID bình luận cha (0 nếu là bình luận gốc)'); // Parent comment ID
            $table->text('quote_content')->nullable()->comment('Nội dung được trích dẫn'); // Quoted content
            $table->integer('like_count')->default(0)->comment('Số lượt thích'); // Like count
            $table->integer('dislike_count')->default(0)->comment('Số lượt không thích'); // Dislike count
            $table->boolean('is_approved')->default(true)->comment('Trạng thái duyệt'); // Approval status
            $table->boolean('is_solution')->default(false)->comment('Đánh dấu là giải pháp'); // Solution flag
            $table->timestamps(); // created_at, updated_at
            $table->softDeletes(); // deleted_at for soft delete

            // Indexes
            $table->index(['topic_id']);
            $table->index(['user_id']);
            $table->index(['parent_id']);
            $table->index(['is_approved']);
            $table->index(['is_solution']);
            $table->index(['created_at']);
            $table->index(['like_count']);
        });

        // Bảng follow
        Schema::create('topic_follows', function (Blueprint $table) {
            $table->id(); // Primary key
            $table->foreignId('user_id')->constrained()->comment('ID người follow'); // Follower ID
            $table->foreignId('topic_id')->constrained('topics')->onDelete('cascade')->comment('ID chủ đề'); // Topic ID
            $table->timestamps(); // created_at, updated_at

            // Unique constraint để tránh trùng lặp
            $table->unique(['user_id', 'topic_id']);

            // Indexes
            $table->index(['user_id']);
            $table->index(['topic_id']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('topic_follows');
        Schema::dropIfExists('topic_comments');
        Schema::dropIfExists('topic_tag_details');
        Schema::dropIfExists('topic_category_details');
        Schema::dropIfExists('topics');
        Schema::dropIfExists('topic_tags');
        Schema::dropIfExists('topic_categories');
    }
};
