<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — Cases Table
//
//  The core learning content of the platform.
//  Each case is a practical business scenario with:
//    - A realistic situation (Egyptian/GCC context)
//    - Challenge questions to think through
//    - A full solution with best practice breakdown
//    - A bigger lesson
//    - Skills taught
//
//  Case structure:
//    hub_id       → which hub (Finance / Marketing / Sales)
//    topic        → which topic track within the hub
//    layer        → generic (applies to any business) OR sector-specific
//    difficulty   → beginner | intermediate | advanced
//    language     → en | ar | both
//
//  case_sectors: many-to-many — a case can apply to multiple sectors
//  case_progress: tracks which cases each user has read
// ══════════════════════════════════════════════════════════════════

return new class extends Migration
{
    public function up(): void
    {
        // ── Cases ──────────────────────────────────────────────
        Schema::create('cases', function (Blueprint $table) {
            $table->id();

            // ── Ownership & Classification ─────────────────────
            $table->foreignId('hub_id')
                  ->constrained('hubs')
                  ->cascadeOnDelete();

            $table->foreignId('created_by')
                  ->constrained('users')
                  ->cascadeOnDelete();

            // ── Topic Track ────────────────────────────────────
            $table->string('topic_slug');                    // cash_flow | budgeting | pricing...
            $table->string('topic_name_en');
            $table->string('topic_name_ar');

            // ── Layer ──────────────────────────────────────────
            $table->enum('layer', [
                'generic',           // applies to any business
                'sector_specific',   // written for a specific industry
            ])->default('generic');

            // ── Difficulty ─────────────────────────────────────
            $table->enum('difficulty', [
                'beginner',          // final year students — hints provided
                'intermediate',      // 1-2 years experience
                'advanced',          // 3-5 years — ambiguous, multiple answers
            ])->default('beginner');

            // ── Case Content (English) ─────────────────────────
            $table->string('title_en');
            $table->string('subtitle_en')->nullable();       // e.g. "The supplier who broke our cash flow"
            $table->text('situation_en');                    // the scenario setup
            $table->json('challenge_questions_en');          // array of questions to think through
            $table->longText('solution_en');                 // full best practice solution
            $table->text('bigger_lesson_en');                // the principle that applies broadly
            $table->json('skills_taught_en');                // array of skills this case teaches

            // ── Case Content (Arabic) ─────────────────────────
            $table->string('title_ar')->nullable();
            $table->string('subtitle_ar')->nullable();
            $table->text('situation_ar')->nullable();
            $table->json('challenge_questions_ar')->nullable();
            $table->longText('solution_ar')->nullable();
            $table->text('bigger_lesson_ar')->nullable();
            $table->json('skills_taught_ar')->nullable();

            // ── Case Data ─────────────────────────────────────
            // Optional financial numbers or tables used in the case
            $table->json('case_data')->nullable();           // numbers, tables for the scenario

            // ── Estimated Reading Time ─────────────────────────
            $table->unsignedTinyInteger('read_time_minutes')->default(10);

            // ── Status ────────────────────────────────────────
            $table->enum('status', [
                'draft',             // being written — not visible to users
                'review',            // ready for Mahmoud to approve
                'published',         // live for members
                'archived',          // retired but kept for reference
            ])->default('draft');

            $table->timestamp('published_at')->nullable();

            // ── Sorting ───────────────────────────────────────
            $table->unsignedSmallInteger('sort_order')->default(0);

            $table->timestamps();
        });

        // ── Case Sectors (many-to-many) ────────────────────────
        Schema::create('case_sectors', function (Blueprint $table) {
            $table->id();
            $table->foreignId('case_id')
                  ->constrained('cases')
                  ->cascadeOnDelete();
            $table->foreignId('sector_id')
                  ->constrained('sectors')
                  ->cascadeOnDelete();
            $table->unique(['case_id', 'sector_id']);
        });

        // ── Case Progress ──────────────────────────────────────
        // Tracks which cases each member has read
        Schema::create('case_progress', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();
            $table->foreignId('case_id')
                  ->constrained('cases')
                  ->cascadeOnDelete();
            $table->enum('status', [
                'started',           // opened but not finished
                'completed',         // read all the way through
            ])->default('started');
            $table->boolean('solution_revealed')->default(false);  // did they read the answer?
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();
            $table->unique(['user_id', 'case_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('case_progress');
        Schema::dropIfExists('case_sectors');
        Schema::dropIfExists('cases');
    }
};
