<?php
declare(strict_types=1);

use Celena\Core\Database\Blueprint;
use Celena\Core\Database\Connection;
use Celena\Core\Database\Schema;

return function (Schema $schema, Connection $conn): void {
    $schema->create('analytics_events', function (Blueprint $t) {
        $t->id();
        $t->string('visitor_id', 40)->default('');
        $t->string('session_id', 40)->default('');
        $t->string('type', 16)->default('pageview');   // pageview | event | leave
        $t->string('event_name', 60)->nullable();
        $t->string('url', 500)->nullable();
        $t->string('referrer', 500)->nullable();
        $t->string('source', 20)->nullable();           // ads | organic | social | referral | direct
        $t->string('utm_source', 120)->nullable();
        $t->string('utm_medium', 120)->nullable();
        $t->string('utm_campaign', 150)->nullable();
        $t->string('utm_term', 150)->nullable();
        $t->string('gclid', 200)->nullable();
        $t->string('device', 10)->nullable();           // mobile | desktop | tablet
        $t->integer('duration')->nullable();            // секунды (для type=leave)
        $t->string('country', 2)->nullable();
        $t->string('ip_hash', 64)->nullable();
        $t->datetime('created_at')->nullable();
        $t->index('visitor_id');
        $t->index('session_id');
        $t->index('type');
        $t->index('source');
        $t->index('created_at');
    });
};
