<?php

declare(strict_types=1);

use App\Jobs\RouteSupplierLeadJob;
use App\Models\Deal;
use App\Models\Project;
use App\Models\Supplier;
use App\Models\SupplierLead;
use App\Models\SystemSetting;
use App\Models\Tenant;
use App\Services\Billing\LedgerService;
use App\Services\LeadDistributor;
use App\Services\LeadRouter;
use App\Services\NotificationService;
use App\Services\RegionTagResolver;
use App\Services\SupplierProjects\SupplierProjectResolver;
use Database\Seeders\PricingTierSeeder;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;

uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);

/**
 * Phase 3 — DIRECT platform end-to-end.
 *
 * Supplier crm.bp-gr.ru шлёт часть лидов на проекты БЕЗ B[123]_ префикса
 * (e.g. `client.carmoney.ru`, `cashmotor.ru`, числовой callback `79135191264`).
 * До Phase 3 такие webhook'и отвергались с 302 redirect и терялись —
 * наблюдалось 67 потерь/день для tenant client1 на проде 25.05.2026.
 *
 * Phase 3 принимает их как platform='DIRECT' end-to-end:
 *   - controller regex снят, parsePlatform возвращает 'DIRECT' для не-B;
 *   - SupplierProjectResolver принимает DIRECT;
 *   - RouteSupplierLeadJob.parseProjectField парсит без B-префикса;
 *   - LeadRouter для DIRECT использует signal_type+identifier match напрямую
 *     (без project_supplier_links pivot — psl-rows для DIRECT не созданы).
 *
 * Spec: docs/superpowers/specs/2026-05-25-supplier-webhook-reliability-design.md §3 Phase 3
 */
beforeEach(function (): void {
    $this->seed(PricingTierSeeder::class);
    DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");

    SystemSetting::query()
        ->where('key', 'supplier_webhook_secret')
        ->update(['value' => 'test-secret-32chars-aaaaaaaaaaaaaa']);
    SystemSetting::query()
        ->where('key', 'supplier_ip_allowlist')
        ->update(['value' => '[]']);
});

function directDispatchJob(int $supplierLeadId): void
{
    (new RouteSupplierLeadJob($supplierLeadId))->handle(
        app(LeadRouter::class),
        app(SupplierProjectResolver::class),
        app(NotificationService::class),
        app(LedgerService::class),
        app(LeadDistributor::class),
        app(RegionTagResolver::class),
    );
}

it('webhook with non-B-prefix project is accepted (202) and platform=DIRECT', function (): void {
    $response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
        'vid' => 9999001,
        'project' => 'client.carmoney.ru',
        'phone' => '79991234567',
        'time' => time(),
    ]);

    $response->assertStatus(202);
    $lead = SupplierLead::where('vid', 9999001)->first();
    expect($lead)->not->toBeNull();
    expect($lead->platform)->toBe('DIRECT');
});

it('SupplierProjectResolver creates DIRECT supplier_project for non-B project', function (): void {
    $resolver = app(SupplierProjectResolver::class);
    $sp = $resolver->resolveOrStub('DIRECT', 'site', 'client.carmoney.ru');

    expect($sp->platform)->toBe('DIRECT');
    expect($sp->unique_key)->toBe('client.carmoney.ru');
    expect($sp->signal_type)->toBe('site');
});

it('RouteSupplierLeadJob delivers DIRECT lead to matching project via signal_identifier fallback', function (): void {
    // Создаём Лидерра-проект с тем же signal_identifier, что и DIRECT-supplier_project.
    // ВАЖНО: НЕ создаём project_supplier_links — Phase 3 fallback должен матчить
    // только по signal_type+signal_identifier.
    $tenant = Tenant::factory()->create([
        'balance_leads' => 0,
        'balance_rub' => '1000.00',
        'delivered_in_month' => 0,
    ]);
    $project = Project::factory()->create([
        'tenant_id' => $tenant->id,
        'signal_type' => 'site',
        'signal_identifier' => 'client.carmoney.ru',
        'is_active' => true,
        'daily_limit_target' => 10,
        'effective_daily_limit_today' => 10,
        'delivered_today' => 0,
        'delivery_days_mask' => 127,
        'region_mask' => 255,
    ]);

    createRoutingSnapshotFromProject($project, signalType: 'site', signalIdentifier: 'client.carmoney.ru');

    $lead = SupplierLead::factory()->create([
        'platform' => 'DIRECT',
        'phone' => '79991234567',
        'vid' => 9999002,
        'raw_payload' => ['vid' => 9999002, 'project' => 'client.carmoney.ru', 'phone' => '79991234567', 'time' => time()],
        'received_at' => now(),
    ]);

    directDispatchJob($lead->id);

    $deal = Deal::where('tenant_id', $tenant->id)
        ->where('phone', '79991234567')
        ->first();
    expect($deal)->not->toBeNull();
    expect($deal->project_id)->toBe($project->id);
    expect($deal->source_crm_id)->toBe(9999002);
});

it('numeric-only project (e.g. 79135191264 callback) accepted as DIRECT', function (): void {
    // Поставщик иногда шлёт project=телефонный номер для callback-проектов.
    $response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
        'vid' => 9999003,
        'project' => '79135191264',
        'phone' => '79991234567',
        'time' => time(),
    ]);

    $response->assertStatus(202);
    $lead = SupplierLead::where('vid', 9999003)->first();
    expect($lead->platform)->toBe('DIRECT');
});

it('existing B1 webhooks still work as platform=B1 (regression)', function (): void {
    $response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
        'vid' => 9999004,
        'project' => 'B1_krk-finance.ru',
        'phone' => '79991234567',
        'time' => time(),
    ]);

    $response->assertStatus(202);
    expect(SupplierLead::where('vid', 9999004)->first()->platform)->toBe('B1');
});

it('SupplierProjectResolver still rejects unknown platforms other than DIRECT', function (): void {
    $resolver = app(SupplierProjectResolver::class);

    expect(fn () => $resolver->resolveOrStub('UNKNOWN', 'site', 'foo.ru'))
        ->toThrow(InvalidArgumentException::class);
});
