<?php

declare(strict_types=1);

use App\Models\PricingTier;
use App\Models\Project;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Tests\Concerns\SharesSupplierPdo;

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

beforeEach(function () {
    Queue::fake();
    DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
    PricingTier::query()->create([
        'tier_no' => 1,
        'leads_in_tier' => null,
        'price_per_lead_kopecks' => 5000, // 50₽/лид — capacity = balance/50
        'is_active' => true,
        'effective_from' => now(),
    ]);
});

it('skips bulk limit increase that would exceed balance capacity', function () {
    // 500₽ / 50₽ = 10 лидов capacity. Два активных проекта по 5 (сумма 10 = ёмкость).
    $tenant = Tenant::factory()->withRequisites()->create(['balance_rub' => '500.00']);
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $p1 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);
    $p2 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);

    // replace=50 каждому → прогнозный суммарный лимит активных 100 > 10 → оба повышения снять.
    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'update_limit',
        'ids' => [$p1->id, $p2->id],
        'replace' => 50,
    ])->assertOk()->assertJson([
        'updated' => 0,
        'skipped' => [
            ['id' => $p1->id, 'reason' => 'balance_insufficient'],
            ['id' => $p2->id, 'reason' => 'balance_insufficient'],
        ],
    ]);

    expect($p1->fresh()->daily_limit_target)->toBe(5); // не изменилось
    expect($p2->fresh()->daily_limit_target)->toBe(5);
});

it('applies bulk limit increase when balance covers it', function () {
    // 5000₽ / 50₽ = 100 capacity. Два проекта по 5 → replace=40 → сумма 80 ≤ 100 → применить.
    $tenant = Tenant::factory()->withRequisites()->create(['balance_rub' => '5000.00']);
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $p1 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);
    $p2 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 5, 'delivered_today' => 0]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'update_limit',
        'ids' => [$p1->id, $p2->id],
        'replace' => 40,
    ])->assertOk()->assertJson(['updated' => 2, 'skipped' => []]);

    expect($p1->fresh()->daily_limit_target)->toBe(40);
    expect($p2->fresh()->daily_limit_target)->toBe(40);
});

it('applies bulk limit decrease even when total still exceeds capacity', function () {
    // Баланс упал: capacity 10, но проекты уже по 50 (историческая сумма 100).
    // delta=-10 → 40 каждому (понижение) — баланс не должен блокировать понижение.
    $tenant = Tenant::factory()->withRequisites()->create(['balance_rub' => '500.00']);
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $p1 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 50, 'delivered_today' => 0]);
    $p2 = Project::factory()->for($tenant)->create(['is_active' => true, 'daily_limit_target' => 50, 'delivered_today' => 0]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'update_limit',
        'ids' => [$p1->id, $p2->id],
        'delta' => -10,
    ])->assertOk()->assertJson(['updated' => 2, 'skipped' => []]);

    expect($p1->fresh()->daily_limit_target)->toBe(40);
    expect($p2->fresh()->daily_limit_target)->toBe(40);
});
