<?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('nova_cards', function (Blueprint $table): void {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('category_id')->nullable()->constrained('nova_card_categories')->nullOnDelete();
            $table->string('title', 120);
            $table->string('slug', 160);
            $table->text('quote_text');
            $table->string('quote_author', 160)->nullable();
            $table->string('quote_source', 180)->nullable();
            $table->text('description')->nullable();
            $table->enum('format', ['square', 'portrait', 'story', 'landscape'])->default('square');
            $table->json('project_json');
            $table->unsignedInteger('render_version')->default(1);
            $table->string('preview_path')->nullable();
            $table->unsignedInteger('preview_width')->nullable();
            $table->unsignedInteger('preview_height')->nullable();
            $table->enum('background_type', ['gradient', 'upload', 'template', 'solid'])->default('gradient');
            $table->foreignId('background_image_id')->nullable()->constrained('nova_card_backgrounds')->nullOnDelete();
            $table->foreignId('template_id')->nullable()->constrained('nova_card_templates')->nullOnDelete();
            $table->enum('visibility', ['public', 'unlisted', 'private'])->default('private');
            $table->enum('status', ['draft', 'processing', 'published', 'hidden', 'rejected'])->default('draft');
            $table->enum('moderation_status', ['pending', 'approved', 'flagged', 'rejected'])->default('pending');
            $table->boolean('featured')->default(false);
            $table->boolean('allow_download')->default(true);
            $table->unsignedInteger('views_count')->default(0);
            $table->unsignedInteger('shares_count')->default(0);
            $table->unsignedInteger('downloads_count')->default(0);
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('user_id');
            $table->index('status');
            $table->index('moderation_status');
            $table->index('category_id');
            $table->index('published_at');
            $table->index('featured');
            $table->index(['visibility', 'status', 'published_at']);
        });

        Schema::create('nova_card_tag_relation', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('card_id')->constrained('nova_cards')->cascadeOnDelete();
            $table->foreignId('tag_id')->constrained('nova_card_tags')->cascadeOnDelete();
            $table->timestamps();

            $table->unique(['card_id', 'tag_id']);
        });
    }

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