<?php

declare(strict_types=1);

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

uses(DatabaseTransactions::class);

it('GET /api/admin/dashboard/leads возвращает KPI лидов', function () {
    $tenant = DB::table('tenants')->insertGetId([
        'subdomain' => 'leadsacme', 'organization_name' => 'Acme', 'contact_email' => 'a@acme.ru',
        'status' => 'active', 'is_trial' => false, 'balance_rub' => 0, 'balance_leads' => 0,
        'chargeback_unrecovered_rub' => 0, 'created_at' => now(), 'updated_at' => now(),
    ]);

    $supplierProjectId = DB::table('supplier_projects')->insertGetId([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'leads-x.ru',
        'current_limit' => 10, 'sync_status' => 'ok', 'created_at' => now(), 'updated_at' => now(),
    ]);

    DB::table('supplier_leads')->insert([
        ['supplier_project_id' => $supplierProjectId, 'platform' => 'B1', 'raw_payload' => '{}',
            'phone' => '79990000001', 'received_at' => now(), 'processed_at' => now(), 'deals_created_count' => 1],
        ['supplier_project_id' => $supplierProjectId, 'platform' => 'B1', 'raw_payload' => '{}',
            'phone' => '79990000002', 'received_at' => now()->subHours(6), 'processed_at' => null, 'deals_created_count' => null],
    ]);

    $res = $this->getJson('/api/admin/dashboard/leads');

    $res->assertOk();
    $res->assertJsonStructure([
        'light',
        'kpi' => ['delivered_today', 'received_today', 'stuck', 'unrouted'],
    ]);
    expect($res->json('kpi.stuck'))->toBe(1);     // 1 зависший (6ч, не обработан)
    expect($res->json('kpi.unrouted'))->toBe(1);  // 1 в очереди
    expect($res->json('light'))->toBe('red');     // есть зависший
});
