<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('media_categories', function (Blueprint $table) {
            $table->id();
            $table->foreignId('parent_id')->nullable()->constrained('media_categories')->nullOnDelete();
            $table->string('name');
            $table->string('type', 30)->default('gallery'); // gallery | document
            $table->integer('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('media_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('project_id')->nullable()->constrained('projects')->nullOnDelete();
            $table->string('uploader_type', 30); // admin, producer, employer
            $table->unsignedBigInteger('uploader_id')->nullable(); // user_id or producer_id
            $table->string('type', 30); // image, video, document
            $table->foreignId('category_id')->nullable()->constrained('media_categories')->nullOnDelete();
            $table->foreignId('subcategory_id')->nullable()->constrained('media_categories')->nullOnDelete();
            $table->string('name')->nullable();
            $table->string('file_path')->nullable(); // single file or first file
            $table->json('files')->nullable(); // multiple files
            $table->text('description')->nullable();
            $table->text('notes')->nullable();
            $table->json('tags')->nullable();
            $table->decimal('gps_lat', 10, 8)->nullable();
            $table->decimal('gps_lng', 11, 8)->nullable();
            $table->string('status', 30)->default('pending'); // pending, approved, rejected, needs_edit
            $table->text('admin_reason')->nullable();
            $table->dateTime('start_at')->nullable(); // for documents
            $table->dateTime('end_at')->nullable();
            $table->string('contract_number')->nullable();
            $table->string('company_name')->nullable();
            $table->string('contact_phone')->nullable();
            $table->dateTime('created_at_file')->nullable(); // تاریخ ایجاد فایل
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('media_items');
        Schema::dropIfExists('media_categories');
    }
};
