<?php

declare(strict_types=1);

use App\Jobs\SyncSupplierProjectJob;
use App\Models\Project;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\Queue;

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

it('updates name+daily_limit without resync', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create([
        'tenant_id' => $tenant->id, 'signal_type' => 'site',
        'signal_identifier' => 'a.ru', 'daily_limit_target' => 10,
    ]);

    $this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
        'name' => 'New name', 'daily_limit_target' => 50,
    ])->assertOk();

    expect($project->fresh()->name)->toBe('New name');
    expect($project->fresh()->daily_limit_target)->toBe(50);
    Queue::assertNotPushed(SyncSupplierProjectJob::class);
});

it('changing sms_senders triggers resync', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create([
        'tenant_id' => $tenant->id, 'signal_type' => 'sms',
        'sms_senders' => ['OLD'], 'sms_keyword' => 'x',
    ]);

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

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

it('rejects daily_limit_target below delivered_today', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create([
        'tenant_id' => $tenant->id, 'daily_limit_target' => 50, 'delivered_today' => 30,
    ]);

    $this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
        'daily_limit_target' => 20,
    ])->assertStatus(422);
});

it('rejects update of signal_type (immutable)', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id, 'signal_type' => 'site', 'signal_identifier' => 'test.ru']);

    $response = $this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
        'signal_type' => 'call',
    ]);

    // signal_type должен быть проигнорирован (не падает 422, но и не меняется)
    expect($project->fresh()->signal_type)->toBe('site');
});

it('cross-tenant update returns 404', function () {
    $tenantA = Tenant::factory()->create();
    $tenantB = Tenant::factory()->create();
    $userA = User::factory()->create(['tenant_id' => $tenantA->id]);
    $project = Project::factory()->create(['tenant_id' => $tenantB->id]);

    $this->actingAs($userA)->patchJson("/api/projects/{$project->id}", [
        'name' => 'hack',
    ])->assertStatus(404);
});

it('updates region_mask and delivery_days_mask', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->create(['tenant_id' => $tenant->id]);
    $project = Project::factory()->create(['tenant_id' => $tenant->id]);

    $this->actingAs($user)->patchJson("/api/projects/{$project->id}", [
        'region_mask' => 78, 'region_mode' => 'exclude', 'delivery_days_mask' => 31,
    ])->assertOk();

    expect($project->fresh()->region_mask)->toBe(78);
});
