<?php

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

return new class extends Migration
{
    protected $connection = 'sam_stat';

    public function up(): void
    {
        Schema::connection('sam_stat')->create('stat_snapshots', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->date('snapshot_date');
            $table->string('domain', 50);
            $table->string('snapshot_type', 50)->default('daily');
            $table->json('data');
            $table->timestamp('created_at')->nullable();

            $table->unique(['tenant_id', 'snapshot_date', 'domain', 'snapshot_type'], 'uk_tenant_date_domain');
            $table->index('snapshot_date', 'idx_date');
        });
    }

    public function down(): void
    {
        Schema::connection('sam_stat')->dropIfExists('stat_snapshots');
    }
};
