<?php

declare(strict_types=1);

use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;

uses(SharesSupplierPdo::class);

it('creates phone_ranges with lookup columns', function (): void {
    expect(DB::selectOne("SELECT to_regclass('public.phone_ranges') AS t")->t)->not->toBeNull();

    $cols = collect(DB::select("SELECT column_name FROM information_schema.columns WHERE table_name = 'phone_ranges'"))
        ->pluck('column_name')->all();

    expect($cols)->toContain('def_code', 'from_num', 'to_num', 'operator', 'region', 'subject_code', 'import_id');
});

it('creates phone_ranges_imports journal table', function (): void {
    expect(DB::selectOne("SELECT to_regclass('public.phone_ranges_imports') AS t")->t)->not->toBeNull();

    $cols = collect(DB::select("SELECT column_name FROM information_schema.columns WHERE table_name = 'phone_ranges_imports'"))
        ->pluck('column_name')->all();

    expect($cols)->toContain('source_url', 'checksum_sha256', 'status', 'rows_inserted', 'rows_updated');
});

it('creates lead_region_resolution_log as a partitioned table', function (): void {
    $partitioned = DB::selectOne(
        "SELECT 1 AS ok
         FROM pg_partitioned_table pt
         JOIN pg_class c ON c.oid = pt.partrelid
         WHERE c.relname = 'lead_region_resolution_log'"
    );

    expect($partitioned)->not->toBeNull();
});

it('adds resolution columns to supplier_leads', function (): void {
    $cols = collect(DB::select("SELECT column_name FROM information_schema.columns WHERE table_name = 'supplier_leads'"))
        ->pluck('column_name')->all();

    expect($cols)->toContain('resolved_subject_code', 'region_source', 'dadata_qc', 'phone_operator');
});

it('adds resolution columns to deals', function (): void {
    $cols = collect(DB::select("SELECT column_name FROM information_schema.columns WHERE table_name = 'deals'"))
        ->pluck('column_name')->all();

    expect($cols)->toContain('phone_operator', 'region_substituted');
});
