<?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('links', function (Blueprint $table) {
            $table->id();
            $table->string('ulid', 26)->unique();
            $table->foreignId('workspace_id')->constrained()->cascadeOnDelete();
            $table->foreignId('domain_id')->nullable()->constrained()->nullOnDelete();
            $table->string('slug', 64);
            $table->text('target_url');
            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->string('favicon_url')->nullable();
            $table->enum('status', ['active', 'disabled', 'expired'])->default('active');
            $table->string('password', 255)->nullable();
            $table->timestamp('expires_at')->nullable();
            $table->unsignedBigInteger('click_limit')->nullable();
            $table->json('rules')->nullable();
            $table->json('pixel_config')->nullable();
            $table->json('tags')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();
            $table->softDeletes();
            $table->timestamps();
            $table->unique(['workspace_id', 'domain_id', 'slug'], 'links_workspace_domain_slug_unique');
            $table->index('workspace_id');
            $table->index('status');
        });
    }

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