<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — Add Share Knowledge columns to forum_posts
//  Location: database/migrations/2025_01_01_000011_add_share_knowledge_to_forum_posts.php
//
//  Three new columns on forum_posts:
//    post_type    → 'question' (Ask Members) | 'knowledge_share' (Share Knowledge)
//    source_url   → optional external link — displayed via /go?url= warning page
//    source_label → optional label — 'LinkedIn' | 'WhatsApp' | 'Article' | 'Other'
//
//  All existing rows default to 'question' — no data change.
//  Run: php artisan migrate
// ══════════════════════════════════════════════════════════════════

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('forum_posts', function (Blueprint $table) {
            // Post type — distinguishes Ask Members from Share Knowledge
            $table->enum('post_type', ['question', 'knowledge_share'])
                  ->default('question')
                  ->after('hub_id');

            // Optional external source URL — stored as plain text
            // Displayed via /go?url= redirect warning page, never as bare <a href>
            $table->string('source_url', 2048)
                  ->nullable()
                  ->after('post_type');

            // Optional human-readable source label
            $table->string('source_label', 50)
                  ->nullable()
                  ->after('source_url');

            // Index post_type — used in every index query filter
            $table->index('post_type');
        });
    }

    public function down(): void
    {
        Schema::table('forum_posts', function (Blueprint $table) {
            $table->dropIndex(['post_type']);
            $table->dropColumn(['post_type', 'source_url', 'source_label']);
        });
    }
};
