<?php

declare(strict_types=1);

/**
 * 152-ФЗ: pd_processing_log 'created' записывается при создании сделки
 * по двум живым путям — ручной API, поставщик (RouteSupplierLeadJob).
 */

use App\Jobs\RouteSupplierLeadJob;
use App\Models\Deal;
use App\Models\Project;
use App\Models\SupplierLead;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Models\User;
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);

beforeEach(function (): void {
    $this->seed(PricingTierSeeder::class);
    DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
});

// ──────────────────────────────────────────────────────────────────────────
// Path A: manual deal creation via DealController::store()
// ──────────────────────────────────────────────────────────────────────────

it('writes pd_processing_log created (manual) when deal created via API', function () {
    $tenant = Tenant::factory()->create(['balance_leads' => 100]);
    $user = User::factory()->for($tenant)->create();
    $this->actingAs($user);

    $before = DB::table('pd_processing_log')->where('purpose', 'lead_create_manual')->count();

    $r = $this->postJson('/api/deals', [
        'project_name' => 'Тест ПД',
        'phone' => '+7 (999) 111-22-33',
    ]);
    $r->assertStatus(201);

    $dealId = $r->json('deal.id');

    $rows = DB::table('pd_processing_log')
        ->where('action', 'created')
        ->where('purpose', 'lead_create_manual')
        ->where('subject_type', 'lead')
        ->where('subject_id', $dealId)
        ->where('tenant_id', $tenant->id)
        ->where('actor_tenant_user_id', $user->id)
        ->whereNull('actor_admin_user_id')
        ->count();

    expect($rows)->toBe(1);
});

// ──────────────────────────────────────────────────────────────────────────
// Path B: supplier integration via RouteSupplierLeadJob
// ──────────────────────────────────────────────────────────────────────────

it('writes pd_processing_log created (supplier) when deal created via RouteSupplierLeadJob', function () {
    $supplier = SupplierProject::factory()->create([
        'platform' => 'B1',
        'signal_type' => 'site',
        'unique_key' => 'pd-test.ru',
    ]);
    $tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
    $project = Project::factory()->create([
        'tenant_id' => $tenant->id,
        'supplier_b1_project_id' => $supplier->id,
        'signal_type' => 'site',
        'signal_identifier' => 'pd-test.ru',
        'is_active' => true,
        'delivered_today' => 0,
        'delivered_in_month' => 0,
    ]);
    linkProjectToSupplier($project, $supplier);
    createRoutingSnapshotFromProject($project, null, 'site', 'pd-test.ru');

    $vid = 77741;
    $lead = SupplierLead::factory()->create([
        'supplier_project_id' => null,
        'platform' => 'B1',
        'vid' => $vid,
        'phone' => '79992223344',
        'raw_payload' => [
            'vid' => $vid,
            'project' => 'B1_pd-test.ru',
            'phone' => '79992223344',
            'time' => now()->getTimestamp(),
        ],
    ]);

    (new RouteSupplierLeadJob($lead->id))->handle(
        app(LeadRouter::class),
        app(SupplierProjectResolver::class),
        app(NotificationService::class),
        app(LedgerService::class),
        app(LeadDistributor::class),
        app(RegionTagResolver::class),
    );

    DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
    $deal = Deal::query()->where('tenant_id', $tenant->id)->where('source_crm_id', $vid)->first();
    expect($deal)->not->toBeNull();

    $rows = DB::table('pd_processing_log')
        ->where('action', 'created')
        ->where('purpose', 'lead_create_supplier')
        ->where('subject_type', 'lead')
        ->where('subject_id', $deal->id)
        ->where('tenant_id', $tenant->id)
        ->whereNull('actor_tenant_user_id')
        ->whereNull('actor_admin_user_id')
        ->count();

    expect($rows)->toBe(1);
});
