<?php

declare(strict_types=1);

use App\Models\Project;
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('hard-deletes an empty project', function () {
    $tenant = Tenant::factory()->create(['balance_leads' => 100]);
    $project = app(ProjectService::class)->create($tenant, [
        'name' => 'Empty', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
        'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
    ]);

    app(ProjectService::class)->delete($project);

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

it('blocks delete when project has deals', function () {
    $tenant = Tenant::factory()->create(['balance_leads' => 100]);
    $project = app(ProjectService::class)->create($tenant, [
        'name' => 'WithDeals', 'signal_type' => 'call', 'signal_identifier' => '79991110000',
        'daily_limit_target' => 5, 'regions' => [], 'delivery_days_mask' => 31,
    ]);
    DB::table('deals')->insert([
        'tenant_id' => $tenant->id, 'project_id' => $project->id, 'phone' => '79990001122',
        'status' => 'new', 'received_at' => now(), 'created_at' => now(),
    ]);

    expect(fn () => app(ProjectService::class)->delete($project))
        ->toThrow(HttpResponseException::class);
    expect(Project::find($project->id))->not->toBeNull();
});
