<?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('dashboard_snapshots', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('company_id');
            $table->date('snapshot_date');

            // Mismas cifras que muestra el admin dashboard (consistencia con la flecha).
            $table->decimal('business_total', 14, 2)->default(0);   // patrimonio
            $table->decimal('cash_base', 14, 2)->default(0);        // caja
            $table->decimal('por_cobrar', 14, 2)->default(0);       // saldo por cobrar
            $table->decimal('overdue', 14, 2)->default(0);          // vencido (PAR)
            $table->decimal('delinquency_rate', 5, 2)->default(0);  // % mora

            $table->timestamp('created_at')->useCurrent();

            $table->unique(['company_id', 'snapshot_date'], 'dashboard_snapshots_company_date_unique');
            $table->foreign('company_id')->references('id')->on('companies')->cascadeOnDelete();
            $table->index('snapshot_date');
        });
    }

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