<?php

declare(strict_types=1);

use App\Jobs\Supplier\CleanupInactiveSupplierProjectsJob;
use App\Models\Project;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Services\Supplier\SupplierPortalClient;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Concerns\SharesSupplierPdo;

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

it('does not mark inactive supplier_project that has pivot link to active project', function () {
    $tenant = Tenant::factory()->create();
    $project = Project::factory()->for($tenant)->create([
        'is_active' => true,
        // легаси FK НЕ заполнены (Plan 3+ архитектура):
        'supplier_b1_project_id' => null,
        'supplier_b2_project_id' => null,
        'supplier_b3_project_id' => null,
    ]);
    $sp = SupplierProject::factory()->create([
        'inactive_since' => null,
    ]);
    DB::table('project_supplier_links')->insert([
        'project_id' => $project->id,
        'supplier_project_id' => $sp->id,
        'platform' => $sp->platform,
        'subject_code' => null,
    ]);

    (new CleanupInactiveSupplierProjectsJob)->handle(app(SupplierPortalClient::class));

    expect($sp->fresh()->inactive_since)->toBeNull();
});

it('marks supplier_project inactive when no pivot link exists', function () {
    $sp = SupplierProject::factory()->create(['inactive_since' => null]);
    // нет project_supplier_links

    (new CleanupInactiveSupplierProjectsJob)->handle(app(SupplierPortalClient::class));

    expect($sp->fresh()->inactive_since)->not->toBeNull();
});
