<?php

declare(strict_types=1);

use App\Models\Project;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Services\LeadRouter;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Concerns\SharesSupplierPdo;

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

it('uses snapshot before 21:00 MSK, snapshot_date = today', function () {
    Carbon::setTestNow('2026-05-28 12:00:00', 'Europe/Moscow');

    $tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'frozen_by_balance_at' => null]);
    $project = Project::factory()->for($tenant)->create([
        'is_active' => false, // ЖИВОЕ состояние — paused
        'delivery_days_mask' => 127,
        'daily_limit_target' => 100,
        'delivered_today' => 0,
    ]);
    $sp = SupplierProject::factory()->create();
    DB::table('project_supplier_links')->insert([
        'project_id' => $project->id, 'supplier_project_id' => $sp->id,
        'platform' => $sp->platform, 'subject_code' => null,
    ]);
    // SNAPSHOT за сегодня имеет проект → роутер должен вернуть, несмотря на is_active=false
    DB::table('project_routing_snapshots')->insert([
        'snapshot_date' => '2026-05-28', 'project_id' => $project->id, 'tenant_id' => $tenant->id,
        'daily_limit' => 10, 'delivery_days_mask' => 127, 'regions' => '{}',
        'signal_type' => 'call', 'expected_volume' => 10, 'delivered_count' => 0,
        'created_at' => now(),
    ]);

    $matched = app(LeadRouter::class)->matchEligibleProjects($sp);

    expect($matched)->toHaveCount(1); // ← это R-01 closure
    Carbon::setTestNow();
});

it('uses snapshot after 21:00 MSK, snapshot_date = tomorrow', function () {
    Carbon::setTestNow('2026-05-28 22:00:00', 'Europe/Moscow');

    $tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'frozen_by_balance_at' => null]);
    $project = Project::factory()->for($tenant)->create([
        'is_active' => true, 'delivery_days_mask' => 127,
        'daily_limit_target' => 100, 'delivered_today' => 0,
    ]);
    $sp = SupplierProject::factory()->create();
    DB::table('project_supplier_links')->insert([
        'project_id' => $project->id, 'supplier_project_id' => $sp->id,
        'platform' => $sp->platform, 'subject_code' => null,
    ]);
    // Snapshot за СЕГОДНЯ (2026-05-28) НЕТ.
    // Snapshot за ЗАВТРА (2026-05-29) есть.
    DB::table('project_routing_snapshots')->insert([
        'snapshot_date' => '2026-05-29', 'project_id' => $project->id, 'tenant_id' => $tenant->id,
        'daily_limit' => 10, 'delivery_days_mask' => 127, 'regions' => '{}',
        'signal_type' => 'call', 'expected_volume' => 10, 'delivered_count' => 0,
        'created_at' => now(),
    ]);

    $matched = app(LeadRouter::class)->matchEligibleProjects($sp);

    expect($matched)->toHaveCount(1); // после 21:00 МСК активен завтрашний snapshot
    Carbon::setTestNow();
});

it('returns 0 if no snapshot exists for active date', function () {
    Carbon::setTestNow('2026-05-28 12:00:00', 'Europe/Moscow');

    $tenant = Tenant::factory()->create(['balance_rub' => '500.00']);
    $project = Project::factory()->for($tenant)->create([
        'is_active' => true, 'delivery_days_mask' => 127, 'daily_limit_target' => 10,
    ]);
    $sp = SupplierProject::factory()->create();
    DB::table('project_supplier_links')->insert([
        'project_id' => $project->id, 'supplier_project_id' => $sp->id,
        'platform' => $sp->platform, 'subject_code' => null,
    ]);
    // НЕТ snapshot за 2026-05-28.

    $matched = app(LeadRouter::class)->matchEligibleProjects($sp);

    expect($matched)->toHaveCount(0); // fail-loud, не fallback на live
    Carbon::setTestNow();
});

it('limit comes from snapshot, not live projects.daily_limit_target', function () {
    Carbon::setTestNow('2026-05-28 12:00:00', 'Europe/Moscow');

    $tenant = Tenant::factory()->create(['balance_rub' => '500.00']);
    $project = Project::factory()->for($tenant)->create([
        'is_active' => true, 'delivery_days_mask' => 127,
        'daily_limit_target' => 100, // живой лимит
        'delivered_today' => 7,
    ]);
    $sp = SupplierProject::factory()->create();
    DB::table('project_supplier_links')->insert([
        'project_id' => $project->id, 'supplier_project_id' => $sp->id,
        'platform' => $sp->platform, 'subject_code' => null,
    ]);
    DB::table('project_routing_snapshots')->insert([
        'snapshot_date' => '2026-05-28', 'project_id' => $project->id, 'tenant_id' => $tenant->id,
        'daily_limit' => 5, // ← snapshot лимит МЕНЬШЕ чем delivered_today=7
        'delivery_days_mask' => 127, 'regions' => '{}',
        'signal_type' => 'call', 'expected_volume' => 5, 'delivered_count' => 0,
        'created_at' => now(),
    ]);

    $matched = app(LeadRouter::class)->matchEligibleProjects($sp);

    expect($matched)->toHaveCount(0); // delivered_today=7 >= snapshot.daily_limit=5
    Carbon::setTestNow();
});
