<?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('clicks', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->foreignId('link_id')->constrained()->cascadeOnDelete();
            $table->foreignId('variant_id')->nullable()->constrained('link_variants')->nullOnDelete();
            $table->unsignedBigInteger('workspace_id');
            $table->timestamp('clicked_at')->useCurrent();
            $table->string('ip_hash', 64);
            $table->string('country', 2)->nullable();
            $table->string('city', 64)->nullable();
            $table->enum('device', ['desktop', 'mobile', 'tablet', 'bot', 'unknown'])->default('unknown');
            $table->string('os', 32)->nullable();
            $table->string('browser', 32)->nullable();
            $table->string('referrer_host', 253)->nullable();
            $table->string('referrer_path', 2048)->nullable();
            $table->string('utm_source', 128)->nullable();
            $table->string('utm_medium', 128)->nullable();
            $table->string('utm_campaign', 128)->nullable();
            $table->string('utm_content', 128)->nullable();
            $table->string('utm_term', 128)->nullable();
            $table->string('language', 10)->nullable();
            $table->index('clicked_at');
            $table->index(['link_id', 'clicked_at']);
            $table->index(['workspace_id', 'clicked_at']);
        });
    }

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