<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — Hubs & Sectors Tables
//
//  Hubs: Finance | Marketing | Sales
//  Each hub is a professional community with its own:
//    - Case library
//    - Forum
//    - Document library
//    - Freelance pool
//
//  Sectors: The industries cases are written for
//    manufacturing, trading, services, hospitality,
//    real_estate, startups, healthcare, education, other
//
//  user_hubs: Many-to-many — one user can join multiple hubs
// ══════════════════════════════════════════════════════════════════

return new class extends Migration
{
    public function up(): void
    {
        // ── Hubs ───────────────────────────────────────────────
        Schema::create('hubs', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();            // finance | marketing | sales
            $table->string('name_en');                   // Finance
            $table->string('name_ar');                   // المالية
            $table->text('description_en')->nullable();
            $table->text('description_ar')->nullable();
            $table->string('icon')->nullable();          // icon name for UI
            $table->string('color')->nullable();         // brand color for the hub
            $table->unsignedTinyInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // ── Sectors ────────────────────────────────────────────
        Schema::create('sectors', function (Blueprint $table) {
            $table->id();
            $table->string('slug')->unique();            // manufacturing | retail | hospitality...
            $table->string('name_en');
            $table->string('name_ar');
            $table->unsignedTinyInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        // ── User Hub Memberships ───────────────────────────────
        Schema::create('user_hubs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();
            $table->foreignId('hub_id')
                  ->constrained('hubs')
                  ->cascadeOnDelete();
            $table->boolean('is_primary')->default(false);   // their main hub
            $table->timestamp('joined_at')->useCurrent();
            $table->unique(['user_id', 'hub_id']);           // can't join same hub twice
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('user_hubs');
        Schema::dropIfExists('sectors');
        Schema::dropIfExists('hubs');
    }
};
