<?php

declare(strict_types=1);

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('resources', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->foreignUuid('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->string('slug', 191);
            $table->string('title', 255);
            $table->text('description')->nullable();
            $table->enum('category', ['theme', 'skin', 'plugin', 'tip', 'manual_pdf'])->default('theme');
            $table->json('tags')->nullable();

            $table->foreignUuid('author_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('file_path', 500)->nullable();
            $table->bigInteger('file_size')->default(0);
            $table->string('mime', 100)->nullable();

            $table->unsignedBigInteger('download_count')->default(0);
            $table->unsignedBigInteger('view_count')->default(0);
            $table->decimal('rating_avg', 3, 2)->default(0);
            $table->unsignedBigInteger('rating_count')->default(0);

            $table->boolean('is_free')->default(true);
            $table->bigInteger('price')->nullable();
            $table->integer('required_member_level')->nullable();

            $table->enum('status', ['draft', 'published', 'archived'])->default('draft');
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['tenant_id', 'slug']);
            $table->index(['tenant_id', 'category', 'status', 'published_at']);
        });
    }

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