<?php

declare(strict_types=1);

/**
 * 152-ФЗ: pd_processing_log 'created' записывается при создании сделки
 * через исторический импорт (HistoricalImportService).
 */

use App\Models\ImportLog;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Import\CsvLeadsParser;
use App\Services\Import\HistoricalImportService;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;

uses(DatabaseTransactions::class, SharesSupplierPdo::class);
// HistoricalImportService → MonthlyPartitionManager → CREATE через pgsql_supplier
// (см. MonthlyPartitionManager::DDL_CONNECTION). Нужен shared PDO.

it('writes pd_processing_log created on historical import for each new deal', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->for($tenant)->create();
    DB::statement('SET app.current_tenant_id = '.$tenant->id);

    $log = ImportLog::create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'filename' => 'leads.csv',
        'file_path' => 'imports/x.csv',
        'dry_run' => false,
    ]);

    $header = "\xEF\xBB\xBF".'id,Проект,Тег проекта,Телефон,Создано,Напоминание,Комментарий,Состояние,Имя';
    $rows = array_merge(
        (new CsvLeadsParser)->parse($header."\n".'9901,Окна,окна,79161000001,2023/07/10 10:00:00,,,Новые,')->rows,
        (new CsvLeadsParser)->parse($header."\n".'9902,Окна,окна,79161000002,2023/07/10 10:00:00,,,Новые,')->rows,
    );

    app(HistoricalImportService::class)->import($tenant->id, $user->id, $log, $rows);

    $pd = DB::table('pd_processing_log')
        ->where('action', 'created')
        ->where('purpose', 'lead_create_import_'.$log->id)
        ->get();

    expect($pd)->toHaveCount(2);

    foreach ($pd as $r) {
        expect($r->subject_type)->toBe('lead')
            ->and((int) $r->actor_tenant_user_id)->toBe($user->id)
            ->and($r->actor_admin_user_id)->toBeNull()
            ->and($r->subject_id)->not->toBeNull()
            ->and((int) $r->tenant_id)->toBe($tenant->id);
    }
});

it('does NOT write pd_processing_log on dry_run import', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->for($tenant)->create();
    DB::statement('SET app.current_tenant_id = '.$tenant->id);

    $log = ImportLog::create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'filename' => 'leads.csv',
        'file_path' => 'imports/x.csv',
        'dry_run' => true,
    ]);

    $header = "\xEF\xBB\xBF".'id,Проект,Тег проекта,Телефон,Создано,Напоминание,Комментарий,Состояние,Имя';
    $rows = (new CsvLeadsParser)->parse($header."\n".'9903,Окна,окна,79161000003,2023/07/10 10:00:00,,,Новые,')->rows;

    app(HistoricalImportService::class)->import($tenant->id, $user->id, $log, $rows);

    $count = DB::table('pd_processing_log')
        ->where('purpose', 'lead_create_import_'.$log->id)
        ->count();

    expect($count)->toBe(0);
});

it('does NOT write pd_processing_log on import UPDATE (idempotent re-import)', function () {
    $tenant = Tenant::factory()->create();
    $user = User::factory()->for($tenant)->create();
    DB::statement('SET app.current_tenant_id = '.$tenant->id);

    $header = "\xEF\xBB\xBF".'id,Проект,Тег проекта,Телефон,Создано,Напоминание,Комментарий,Состояние,Имя';

    // First import — creates the deal
    $log1 = ImportLog::create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'filename' => 'leads.csv',
        'file_path' => 'imports/x.csv',
        'dry_run' => false,
    ]);
    $rows1 = (new CsvLeadsParser)->parse($header."\n".'9904,Окна,окна,79161000004,2023/07/10 10:00:00,,,Новые,')->rows;
    app(HistoricalImportService::class)->import($tenant->id, $user->id, $log1, $rows1);

    // Second import — updates the same deal
    $log2 = ImportLog::create([
        'tenant_id' => $tenant->id,
        'user_id' => $user->id,
        'filename' => 'leads2.csv',
        'file_path' => 'imports/x2.csv',
        'dry_run' => false,
    ]);
    $rows2 = (new CsvLeadsParser)->parse($header."\n".'9904,Окна,окна,79161000004,2023/07/10 10:00:00,,,Оплачено,')->rows;
    app(HistoricalImportService::class)->import($tenant->id, $user->id, $log2, $rows2);

    // Only the first import wrote a pd log entry
    $countLog1 = DB::table('pd_processing_log')
        ->where('action', 'created')
        ->where('purpose', 'lead_create_import_'.$log1->id)
        ->count();
    $countLog2 = DB::table('pd_processing_log')
        ->where('action', 'created')
        ->where('purpose', 'lead_create_import_'.$log2->id)
        ->count();

    expect($countLog1)->toBe(1)
        ->and($countLog2)->toBe(0);
});
