<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — Documents Library Table
//
//  Shared knowledge base where members upload professional
//  documents for others to download and learn from.
//
//  Unlike the old project's documents table, this is simple:
//  no OCR, no AI extraction, no financial analysis pipeline.
//  Just upload, share, download, rate.
//
//  What members can share:
//    - Industry reports
//    - Market research PDFs
//    - Templates (financial models, marketing plans)
//    - Best practice guides
//    - News articles saved as PDF
//
//  Visibility:
//    all  → any member on the platform
//    hub  → only members of the same hub
// ══════════════════════════════════════════════════════════════════

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

            // ── Ownership ──────────────────────────────────────
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            $table->foreignId('hub_id')
                  ->constrained('hubs')
                  ->cascadeOnDelete();

            // ── File Info ──────────────────────────────────────
            $table->string('original_filename');             // what user uploaded
            $table->string('stored_filename');               // what we saved it as (UUID-based)
            $table->string('file_path');                     // storage path
            $table->string('file_type', 10);                 // pdf | docx | xlsx
            $table->unsignedBigInteger('file_size');         // in bytes

            // ── Document Info ──────────────────────────────────
            $table->string('title_en');
            $table->string('title_ar')->nullable();
            $table->text('description_en')->nullable();
            $table->text('description_ar')->nullable();
            $table->string('language', 2)->default('en');    // en | ar

            // ── Classification ─────────────────────────────────
            $table->string('document_category');             // report | template | guide | news | other
            $table->json('tags')->nullable();                 // searchable tags

            // ── Sector (optional) ─────────────────────────────
            $table->foreignId('sector_id')
                  ->nullable()
                  ->constrained('sectors')
                  ->nullOnDelete();

            // ── Visibility ────────────────────────────────────
            $table->enum('visibility', ['all', 'hub'])->default('all');

            // ── Status ────────────────────────────────────────
            $table->enum('status', [
                'pending',           // waiting for admin approval
                'approved',          // visible to members
                'rejected',          // admin removed it
            ])->default('pending');

            // ── Counters ──────────────────────────────────────
            $table->unsignedInteger('download_count')->default(0);
            $table->unsignedInteger('rating_count')->default(0);
            $table->decimal('rating_average', 3, 2)->default(0.00); // avg star rating

            $table->timestamps();
        });

        // ── Document Ratings ───────────────────────────────────
        Schema::create('document_ratings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();
            $table->foreignId('document_id')
                  ->constrained('documents')
                  ->cascadeOnDelete();
            $table->unsignedTinyInteger('rating');           // 1-5 stars
            $table->text('comment')->nullable();
            $table->timestamps();
            $table->unique(['user_id', 'document_id']);      // one rating per user per document
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('document_ratings');
        Schema::dropIfExists('documents');
    }
};
