<?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('tag_interaction_daily_metrics', function (Blueprint $table): void {
            $table->id();
            $table->date('metric_date');
            $table->string('surface', 32);
            $table->string('tag_slug', 120)->default('');
            $table->string('source_tag_slug', 120)->default('');
            $table->string('query', 120)->default('');
            $table->unsignedInteger('clicks')->default(0);
            $table->unsignedInteger('unique_users')->default(0);
            $table->unsignedInteger('unique_sessions')->default(0);
            $table->decimal('avg_position', 8, 2)->default(0);
            $table->timestamps();

            $table->unique(
                ['metric_date', 'surface', 'tag_slug', 'source_tag_slug', 'query'],
                'tag_interaction_daily_metrics_unique_idx'
            );
            $table->index(['metric_date', 'surface'], 'tag_interaction_daily_metrics_surface_idx');
            $table->index(['metric_date', 'tag_slug'], 'tag_interaction_daily_metrics_tag_idx');
            $table->index(['metric_date', 'query'], 'tag_interaction_daily_metrics_query_idx');
        });
    }

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