<?php

declare(strict_types=1);

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;

uses(DatabaseTransactions::class);

beforeEach(function () {
    // Чистые счётчики: убираем seed/прошлые данные (FK-порядок: сначала проводки).
    DB::table('balance_transactions')->delete();
    DB::table('tenants')->delete();
});

it('GET /api/admin/dashboard returns finance + health tiles', function () {
    $tenant = DB::table('tenants')->insertGetId([
        'subdomain' => 'acme', 'organization_name' => 'Acme', 'contact_email' => 'a@acme.ru',
        'status' => 'active', 'is_trial' => false, 'balance_rub' => -500, 'balance_leads' => 0,
        'chargeback_unrecovered_rub' => 0, 'created_at' => now(), 'updated_at' => now(),
    ]);
    DB::table('balance_transactions')->insert([
        ['tenant_id' => $tenant, 'type' => 'topup', 'amount_rub' => 10000, 'created_at' => now()],
        ['tenant_id' => $tenant, 'type' => 'lead_charge', 'amount_rub' => -3000, 'created_at' => now()],
    ]);

    $res = $this->getJson('/api/admin/dashboard?period=30d');

    $res->assertOk();
    $res->assertJsonStructure([
        'finance' => ['topups_rub', 'charges_rub', 'active_clients', 'new_clients', 'negative_balance_count', 'light'],
        'health' => ['light', 'open_incidents', 'last_sync_status'],
        'period',
    ]);
    expect($res->json('finance.negative_balance_count'))->toBe(1);
    expect($res->json('finance.light'))->toBe('red');
});

it('summary включает плитки leads и supply', function () {
    $res = $this->getJson('/api/admin/dashboard?period=30d');

    $res->assertOk();
    $res->assertJsonStructure([
        'finance' => ['light'],
        'health' => ['light'],
        'leads' => ['light', 'delivered_today', 'received_today', 'stuck', 'unrouted'],
        'supply' => ['light', 'demand', 'formula', 'ordered', 'mismatches', 'total_orders', 'total_limit'],
        'period',
    ]);
});
