<?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
    {
        Schema::create('disk_items', function (Blueprint $table) {
            $table->id();

            // Folder relationship
            $table->unsignedBigInteger('folder_id');

            // Basic Information
            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->text('alt_text')->nullable(); // For SEO and accessibility
            $table->json('tags')->nullable(); // Image tags for search

            // File Information
            $table->string('original_name'); // Original filename
            $table->string('filename'); // Stored filename
            $table->string('file_path'); // Full file path
            $table->string('file_url'); // Public URL
            $table->string('mime_type');
            $table->string('extension', 20);
            $table->unsignedBigInteger('file_size'); // Size in bytes
            $table->enum('file_type', ['image', 'document', 'video', 'audio', 'archive', 'spreadsheet', 'presentation', 'other'])->default('other'); // File category
            $table->string('file_hash', 64)->nullable(); // SHA-256 hash for duplicate detection

            // Image-specific Dimensions (only for images)
            $table->unsignedInteger('width')->nullable();
            $table->unsignedInteger('height')->nullable();
            $table->decimal('aspect_ratio', 5, 3)->nullable(); // width/height
            $table->enum('orientation', ['landscape', 'portrait', 'square'])->nullable();

            // Thumbnails & Previews (for all file types)
            $table->json('thumbnails')->nullable(); // Different sized versions
            $table->string('thumbnail_path')->nullable(); // Main thumbnail/preview path
            $table->boolean('has_webp')->default(false); // WebP version available (for images)
            $table->string('preview_type')->nullable(); // Type of preview available (thumbnail, pdf_preview, icon, etc.)

            // Processing Information
            $table->boolean('is_processed')->default(false);
            $table->json('processing_data')->nullable(); // EXIF, color analysis, document properties, etc.
            $table->string('dominant_color', 7)->nullable(); // Hex color (for images)
            $table->json('color_palette')->nullable(); // Color palette array (for images)

            // Document-specific Information
            $table->unsignedInteger('page_count')->nullable(); // For PDFs, documents
            $table->unsignedInteger('word_count')->nullable(); // For text documents
            $table->string('language')->nullable(); // Document language
            $table->text('extracted_text')->nullable(); // OCR or extracted text for search

            // Usage & Relations
            $table->json('usage_references')->nullable(); // Where this file is used
            $table->unsignedInteger('usage_count')->default(0);
            $table->boolean('is_featured')->default(false);
            $table->integer('sort_order')->default(0);

            // File Properties
            $table->enum('quality', ['low', 'medium', 'high', 'original'])->default('high');
            $table->json('metadata')->nullable(); // EXIF, document properties, video metadata, etc.

            // Version Control
            $table->unsignedBigInteger('parent_version_id')->nullable(); // Reference to previous version
            $table->integer('version_number')->default(1);
            $table->boolean('is_latest_version')->default(true);
            $table->text('version_notes')->nullable(); // Change notes for this version

            // Copyright & Legal
            $table->string('copyright')->nullable();
            $table->string('author')->nullable(); // Author/creator/photographer
            $table->string('source')->nullable(); // Where file came from
            $table->timestamp('created_date')->nullable(); // When file was originally created (not uploaded)
            $table->json('location_data')->nullable(); // GPS coordinates if available

            // Status & Visibility
            $table->boolean('is_active')->default(true);
            $table->boolean('is_public')->default(true);
            $table->boolean('is_approved')->default(true);
            $table->timestamp('approved_at')->nullable();
            $table->unsignedBigInteger('approved_by')->nullable();

            // Optimization
            $table->boolean('is_optimized')->default(false);
            $table->unsignedInteger('original_size')->nullable(); // Original file size before optimization
            $table->decimal('compression_ratio', 5, 2)->nullable(); // How much was compressed

            // Analytics
            $table->unsignedInteger('view_count')->default(0);
            $table->unsignedInteger('download_count')->default(0);
            $table->timestamp('last_viewed_at')->nullable();
            $table->timestamp('last_downloaded_at')->nullable();

            // Audit Fields
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign Keys
            $table->foreign('folder_id')->references('id')->on('disk_folders')->onDelete('cascade');
            $table->foreign('parent_version_id')->references('id')->on('disk_items')->onDelete('set null');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // Indexes
            $table->index(['folder_id', 'is_active']);
            $table->index(['is_active', 'is_public', 'is_approved']);
            $table->index(['is_featured', 'sort_order']);
            $table->index(['file_size', 'mime_type']);
            $table->index(['file_type', 'is_active']);
            $table->index(['extension']);
            $table->index(['file_hash']); // For duplicate detection
            $table->index(['width', 'height']);
            $table->index(['created_at']);
            $table->index(['view_count', 'download_count']);
            $table->index(['orientation', 'aspect_ratio']);
            $table->index(['version_number', 'is_latest_version']);
            $table->index(['parent_version_id']);

            // Full-text search index
            $table->fullText(['title', 'description', 'alt_text', 'original_name']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('disk_items');
    }
};
