<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::connection('ecommerce')->create('analytics_product_visits', function (Blueprint $table) {
            $table->id();

            // Relasi dasar
            $table->unsignedBigInteger('item_id')->index();
            $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->string('session_id')->nullable()->index();

            // Tracking waktu
            $table->timestamp('started_at')->index();
            $table->timestamp('ended_at')->nullable();
            $table->integer('duration_seconds')->nullable();

            // Optional analytics tambahan
            $table->string('ip_address', 45)->nullable();
            $table->string('device_type')->nullable();
            $table->text('user_agent')->nullable();
            $table->string('referrer')->nullable();

            $table->timestamps();

            // Index tambahan untuk performa
            $table->index(['item_id', 'started_at']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::connection('ecommerce')->dropIfExists('analytics_product_visits');
    }
};
