<?php

declare(strict_types=1);

use App\Models\Project;
use App\Models\SupplierProject;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Concerns\SharesSupplierPdo;

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

it('links project to supplier projects via belongsToMany pivot', function (): void {
    $project = Project::factory()->create();
    $sp1 = SupplierProject::query()->create([
        'platform' => 'B1', 'signal_type' => 'site', 'unique_key' => 'rel.ru',
        'subject_code' => 82, 'current_limit' => 0, 'sync_status' => 'ok',
    ]);
    $sp2 = SupplierProject::query()->create([
        'platform' => 'B2', 'signal_type' => 'site', 'unique_key' => 'rel.ru',
        'subject_code' => 82, 'current_limit' => 0, 'sync_status' => 'ok',
    ]);

    $project->supplierProjects()->attach([
        $sp1->id => ['platform' => 'B1', 'subject_code' => 82],
        $sp2->id => ['platform' => 'B2', 'subject_code' => 82],
    ]);

    expect($project->supplierProjects()->count())->toBe(2)
        // @phpstan-ignore-next-line argument.type — qualified 'projects.id' (belongsToMany disambiguator)
        ->and($sp1->projects()->pluck('projects.id')->all())->toContain($project->id)
        // @phpstan-ignore-next-line property.notFound — withPivot adds dynamic 'pivot' accessor
        ->and($project->supplierProjects->first()->pivot->platform)->not->toBeNull();
});
