<?php

declare(strict_types=1);

use App\Models\Deal;
use App\Models\LeadCharge;
use App\Models\Project;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;

uses(DatabaseTransactions::class);

beforeEach(function () {
    // Создаём testing_rls_user если нет — паттерн из RlsSmokeTest для проверки
    // RLS из не-superuser контекста (postgres BYPASSRLS обходит политику).
    DB::unprepared(<<<'SQL'
DO $$
BEGIN
    IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'testing_rls_user') THEN
        CREATE ROLE testing_rls_user NOLOGIN;
        GRANT USAGE ON SCHEMA public TO testing_rls_user;
        GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO testing_rls_user;
        GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO testing_rls_user;
    END IF;
END
$$;
SQL);

    DB::statement('GRANT SELECT, INSERT ON TABLE lead_charges TO testing_rls_user');
    DB::statement('GRANT USAGE, SELECT ON SEQUENCE lead_charges_id_seq TO testing_rls_user');
});

test('LeadCharge can be created via factory', function () {
    $tenant = Tenant::factory()->create();
    $project = Project::factory()->for($tenant)->create();
    $deal = Deal::factory()->for($tenant)->for($project)->create();

    DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");

    $charge = LeadCharge::factory()->create([
        'tenant_id' => $tenant->id,
        'deal_id' => $deal->id,
        'deal_received_at' => $deal->received_at,
    ]);

    expect($charge->tier_no)->toBeInt();
    expect($charge->price_per_lead_kopecks)->toBeInt();
});

test('LeadCharge belongs to tenant', function () {
    $tenant = Tenant::factory()->create();
    $project = Project::factory()->for($tenant)->create();
    $deal = Deal::factory()->for($tenant)->for($project)->create();

    DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");

    $charge = LeadCharge::factory()->create([
        'tenant_id' => $tenant->id,
        'deal_id' => $deal->id,
        'deal_received_at' => $deal->received_at,
    ]);

    expect($charge->tenant)->toBeInstanceOf(Tenant::class);
    expect($charge->tenant->id)->toBe($tenant->id);
});

test('LeadCharge belongs to deal via deal_id', function () {
    $tenant = Tenant::factory()->create();
    $project = Project::factory()->for($tenant)->create();
    $deal = Deal::factory()->for($tenant)->for($project)->create();

    DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");

    $charge = LeadCharge::factory()->create([
        'tenant_id' => $tenant->id,
        'deal_id' => $deal->id,
        'deal_received_at' => $deal->received_at,
    ]);

    expect($charge->deal)->toBeInstanceOf(Deal::class);
    expect($charge->deal->id)->toBe($deal->id);
});

test('priceRubles accessor', function () {
    $tenant = Tenant::factory()->create();
    $project = Project::factory()->for($tenant)->create();
    $deal = Deal::factory()->for($tenant)->for($project)->create();

    DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");

    $charge = LeadCharge::factory()->create([
        'tenant_id' => $tenant->id,
        'deal_id' => $deal->id,
        'deal_received_at' => $deal->received_at,
        'price_per_lead_kopecks' => 5500,
    ]);

    expect($charge->price_rubles)->toBe(55.0);
});
