<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\Company;
use App\Models\DashboardSnapshot;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Artisan;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class SnapshotDashboardMetricsTest extends TestCase
{
    use DatabaseTransactions;

    #[Test]
    public function it_writes_one_idempotent_snapshot_per_company_per_day(): void
    {
        $company = Company::factory()->create(['status' => Company::STATUS_ACTIVE]);

        Artisan::call('credify:snapshot-dashboard', ['--company' => $company->id]);

        $this->assertDatabaseHas('dashboard_snapshots', [
            'company_id' => $company->id,
            'snapshot_date' => now()->toDateString(),
        ]);

        // Idempotente: re-correr no duplica.
        // Se cuenta SOLO la empresa del test: contar la tabla completa hacia
        // fallar esto en cuanto la BD tuviera snapshots de otras empresas.
        Artisan::call('credify:snapshot-dashboard', ['--company' => $company->id]);
        $this->assertSame(1, DashboardSnapshot::where('company_id', $company->id)->count());

        // Los valores reflejan getMetrics (empresa vacía → ceros).
        $snap = DashboardSnapshot::where('company_id', $company->id)->first();
        $this->assertNotNull($snap);
        $this->assertEquals(0.0, $snap->business_total);
        $this->assertEquals(0.0, $snap->delinquency_rate);
    }
}
