<?php

declare(strict_types=1);

use App\Jobs\Supplier\DeleteSupplierProjectJob;
use App\Jobs\SyncSupplierProjectJob;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Services\Project\ProjectService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;

uses(DatabaseTransactions::class);

beforeEach(fn () => Queue::fake());

it('blocks update that collides source with another project of same tenant', function () {
    $tenant = Tenant::factory()->create(['balance_leads' => 100]);
    $svc = app(ProjectService::class);
    $a = $svc->create($tenant, ['name' => 'A', 'signal_type' => 'call', 'signal_identifier' => '79991110000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
    $b = $svc->create($tenant, ['name' => 'B', 'signal_type' => 'call', 'signal_identifier' => '79992220000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);

    expect(fn () => $svc->update($b, ['signal_identifier' => '79991110000']))
        ->toThrow(HttpResponseException::class);
});

it('allows update keeping same source on the same project', function () {
    $tenant = Tenant::factory()->create(['balance_leads' => 100]);
    $svc = app(ProjectService::class);
    $a = $svc->create($tenant, ['name' => 'A', 'signal_type' => 'call', 'signal_identifier' => '79991110000', 'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31]);
    $updated = $svc->update($a, ['signal_identifier' => '79991110000', 'daily_limit_target' => 7]);
    expect($updated->daily_limit_target)->toBe(7);
});

it('changing source detaches old supplier_projects and dispatches their cleanup (#8)', function () {
    // #8/#9 regression: changing signal_identifier left the old supplier_projects orphan
    // (still linked via pivot, still alive at the supplier) — that is the "two projects
    // instead of one" the owner reported. Fix: detach old key's sps from this project +
    // dispatch DeleteSupplierProjectJob (cleans portal only if no other consumer remains).
    $tenant = Tenant::factory()->create(['balance_leads' => 100]);
    $svc = app(ProjectService::class);
    $p = $svc->create($tenant, [
        'name' => 'X', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
        'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
    ]);

    // Simulate the old source already synced — 3 sps linked via pivot + legacy FKs.
    $oldIds = [];
    foreach (['B1' => 'OLD1', 'B2' => 'OLD2', 'B3' => 'OLD3'] as $platform => $ext) {
        $sp = SupplierProject::create([
            'platform' => $platform, 'signal_type' => 'call', 'unique_key' => '79991110000',
            'subject_code' => null, 'supplier_external_id' => $ext, 'current_limit' => 2,
            'current_workdays' => [1, 2, 3, 4, 5], 'current_regions' => [], 'sync_status' => 'ok',
            'last_synced_at' => now(),
        ]);
        DB::table('project_supplier_links')->insert([
            'project_id' => $p->id, 'supplier_project_id' => $sp->id, 'platform' => $platform, 'subject_code' => null,
        ]);
        $oldIds[] = $sp->id;
        $col = 'supplier_'.strtolower($platform).'_project_id';
        $p->{$col} = $sp->id;
    }
    $p->save();

    // SupplierSnapshotGuard блокирует смену источника на активном+связанном проекте
    // (защита от убытка, пока поставщик может слать лиды по старому слепку — 26.05.2026).
    // Чтобы сменить источник, проект надо снять с публикации и выждать grace; ставим
    // paused_at в прошлое (вне grace), чтобы isProtected()=false и сработал detach-путь.
    DB::table('projects')->where('id', $p->id)->update(['is_active' => false, 'paused_at' => now()->subDays(3)]);
    $p->refresh();

    // Change source — should detach old sps, dispatch their cleanup, and dispatch the new-source sync.
    $svc->update($p, ['signal_identifier' => '79993330000']);

    // Old sps no longer linked to this project via pivot.
    $stillLinked = DB::table('project_supplier_links')
        ->where('project_id', $p->id)
        ->whereIn('supplier_project_id', $oldIds)
        ->count();
    expect($stillLinked)->toBe(0);

    // Legacy FK columns pointing at old sps are cleared.
    $fresh = $p->fresh();
    expect($fresh->supplier_b1_project_id)->toBeNull();
    expect($fresh->supplier_b2_project_id)->toBeNull();
    expect($fresh->supplier_b3_project_id)->toBeNull();

    // Cleanup of OLD sps dispatched (delete-or-resync per remaining-consumers rule).
    Queue::assertPushed(DeleteSupplierProjectJob::class, function ($job) use ($oldIds) {
        return ! array_diff($oldIds, $job->supplierProjectIds);
    });

    // The new source still gets a sync dispatch (existing needsResync path).
    Queue::assertPushed(SyncSupplierProjectJob::class, fn ($job) => $job->projectId === $p->id);
});
