<?php

declare(strict_types=1);

use App\Models\SupplierLead;
use App\Models\SupplierProject;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Carbon;

uses(DatabaseTransactions::class);

it('factory creates a valid SupplierLead', function (): void {
    $supplierProject = SupplierProject::factory()->create([
        'platform' => 'B1',
        'signal_type' => 'site',
        'unique_key' => 'vashinvestor.ru',
    ]);

    $lead = SupplierLead::factory()->create([
        'supplier_project_id' => $supplierProject->id,
        'platform' => 'B1',
    ]);

    expect($lead->id)->toBeInt()->toBeGreaterThan(0);
    expect($lead->platform)->toBe('B1');
    expect($lead->raw_payload)->toBeArray();
    expect($lead->received_at)->toBeInstanceOf(Carbon::class);
});

it('belongsTo supplier_project', function (): void {
    $supplierProject = SupplierProject::factory()->create();
    $lead = SupplierLead::factory()->create([
        'supplier_project_id' => $supplierProject->id,
    ]);

    expect($lead->supplierProject->id)->toBe($supplierProject->id);
});

it('casts raw_payload to array and *_at to Carbon', function (): void {
    $lead = SupplierLead::factory()->create([
        'raw_payload' => ['vid' => 12345, 'project' => 'B1_test.ru'],
        'processed_at' => now(),
    ]);

    expect($lead->raw_payload)->toBe(['vid' => 12345, 'project' => 'B1_test.ru']);
    expect($lead->processed_at)->toBeInstanceOf(Carbon::class);
});

it('factory respects deals_created_count nullable default', function (): void {
    $lead = SupplierLead::factory()->create();
    expect($lead->deals_created_count)->toBeNull();
    expect($lead->processed_at)->toBeNull();
});
