<?php

declare(strict_types=1);

use App\Jobs\SyncSupplierProjectJob;
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;

uses(DatabaseTransactions::class);

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

it('destroy hard-deletes a project with no deals', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);

    $this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertNoContent();

    expect(Project::find($project->id))->toBeNull();
});

it('destroy returns 422 if project has deals', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
    DB::table('deals')->insert([
        'tenant_id' => $tenant->id, 'project_id' => $project->id,
        'phone' => '79990001100', 'status' => 'new',
        'received_at' => now(), 'created_at' => now(),
    ]);

    $this->actingAs($user)->deleteJson("/api/projects/{$project->id}")->assertStatus(422);

    expect(Project::find($project->id))->not->toBeNull();
});

it('sync re-dispatches SyncSupplierProjectJob', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id]);

    $this->actingAs($user)->postJson("/api/projects/{$project->id}/sync")
        ->assertStatus(202)
        ->assertJsonPath('queued', true);

    Queue::assertPushed(SyncSupplierProjectJob::class);
});

it('toggle-active flips is_active flag', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);

    $this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", ['is_active' => false])
        ->assertOk();

    expect($project->fresh()->is_active)->toBeFalse();
});

it('toggle-active dispatches supplier sync so pause/resume reaches the supplier (#10)', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);

    $this->actingAs($user)->patchJson("/api/projects/{$project->id}/toggle-active", ['is_active' => false])
        ->assertOk();

    Queue::assertPushed(SyncSupplierProjectJob::class, fn ($job) => $job->projectId === $project->id);
});

it('bulk pause dispatches supplier sync for each affected project (#10)', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $p1 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
    $p2 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'pause', 'ids' => [$p1->id, $p2->id],
    ])->assertOk()->assertJsonPath('updated', 2);

    Queue::assertPushed(SyncSupplierProjectJob::class, fn ($job) => in_array($job->projectId, [$p1->id, $p2->id], true));
});

it('bulk pause sets is_active=false on multiple', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $p1 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);
    $p2 = Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'pause', 'ids' => [$p1->id, $p2->id],
    ])->assertOk()->assertJsonPath('updated', 2);

    expect($p1->fresh()->is_active)->toBeFalse();
    expect($p2->fresh()->is_active)->toBeFalse();
});

it('bulk filters out cross-tenant ids silently', function () {
    $tenantA = Tenant::factory()->create();
    $tenantB = Tenant::factory()->create();
    $userA = User::factory()->create(['tenant_id' => $tenantA->id]);
    $pA = Project::factory()->create(['tenant_id' => $tenantA->id, 'is_active' => true]);
    $pB = Project::factory()->create(['tenant_id' => $tenantB->id, 'is_active' => true]);

    $this->actingAs($userA)->postJson('/api/projects/bulk', [
        'action' => 'pause', 'ids' => [$pA->id, $pB->id],
    ])->assertOk()->assertJsonPath('updated', 1);

    expect($pB->fresh()->is_active)->toBeTrue();
});

it('bulk delete removes project with no deals', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $p1 = Project::factory()->create(['tenant_id' => $tenant->id]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'delete', 'ids' => [$p1->id],
    ])->assertOk()->assertJsonPath('updated', 1);

    expect(Project::find($p1->id))->toBeNull();
});

it('bulk rejects > 500 ids', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'pause', 'ids' => range(1, 501),
    ])->assertStatus(422);
});

it('bulk rejects unknown action', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);

    $this->actingAs($user)->postJson('/api/projects/bulk', [
        'action' => 'destroy_all', 'ids' => [1],
    ])->assertStatus(422);
});
