<?php

declare(strict_types=1);

use App\Models\BalanceTransaction;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;

uses(DatabaseTransactions::class);

function makeBillingTenant(array $overrides = []): int
{
    return (int) DB::table('tenants')->insertGetId(array_merge([
        'subdomain' => 'bt-'.bin2hex(random_bytes(4)),
        'organization_name' => 'Billing Test Co',
        'contact_email' => 'bt-'.bin2hex(random_bytes(3)).'@test.local',
        'status' => 'active',
        'balance_rub' => '5000.00',
        'is_trial' => false,
        'created_at' => now(),
    ], $overrides));
}

function makeTariffPlan(array $overrides = []): int
{
    return (int) DB::table('tariff_plans')->insertGetId(array_merge([
        'code' => 'test-'.bin2hex(random_bytes(4)),
        'name' => 'Test Plan',
        'billing_model' => 'monthly',
        'price_monthly' => '999.00',
        'created_at' => now(),
    ], $overrides));
}

test('GET tariff-plans возвращает список планов', function () {
    $planId = makeTariffPlan(['name' => 'Visible Plan', 'price_monthly' => '1500.00']);
    $r = $this->getJson('/api/admin/billing/tariff-plans');
    $r->assertOk();
    $plans = $r->json('plans');
    expect($plans)->toBeArray();
    $found = collect($plans)->first(fn ($p) => $p['id'] === $planId);
    expect($found)->not->toBeNull();
    expect($found['id'])->toBeInt();
    expect($found['name'])->toBeString();
    expect($found['price_monthly'])->toBeString();
});

test('PATCH status suspended меняет статус + пишет audit-log', function () {
    $id = makeBillingTenant(['status' => 'active']);
    $r = $this->patchJson("/api/admin/billing/tenants/{$id}/status", [
        'status' => 'suspended',
        'reason' => 'Просрочка оплаты более 30 дней.',
    ]);
    $r->assertOk();
    expect($r->json('status'))->toBe('suspended');
    expect(DB::table('tenants')->where('id', $id)->value('status'))->toBe('suspended');
    expect(DB::table('saas_admin_audit_log')->where('action', 'tenant.suspend')->where('target_id', $id)->exists())->toBeTrue();
});

test('PATCH status active разблокирует', function () {
    $id = makeBillingTenant(['status' => 'suspended']);
    $this->patchJson("/api/admin/billing/tenants/{$id}/status", [
        'status' => 'active', 'reason' => 'Оплата получена, блокировка снята.',
    ])->assertOk();
    expect(DB::table('tenants')->where('id', $id)->value('status'))->toBe('active');
});

test('PATCH status reason короче 10 символов → 422', function () {
    $id = makeBillingTenant();
    $this->patchJson("/api/admin/billing/tenants/{$id}/status", ['status' => 'suspended', 'reason' => 'мало'])
        ->assertStatus(422);
});

test('PATCH status несуществующий тенант → 404', function () {
    $this->patchJson('/api/admin/billing/tenants/99999999/status', [
        'status' => 'suspended', 'reason' => 'Любое основание длиной более десяти.',
    ])->assertStatus(404);
});

test('PATCH status soft-deleted тенант → 404', function () {
    $id = makeBillingTenant(['deleted_at' => now()]);
    $this->patchJson("/api/admin/billing/tenants/{$id}/status", [
        'status' => 'suspended', 'reason' => 'Любое основание длиной более десяти.',
    ])->assertStatus(404);
});

test('POST refund списывает с баланса + создаёт balance_transactions refund', function () {
    $id = makeBillingTenant(['balance_rub' => '5000.00']);
    $r = $this->postJson("/api/admin/billing/tenants/{$id}/refund", [
        'amount_rub' => 1500, 'reason' => 'Возврат по обращению клиента №42.',
    ]);
    $r->assertOk();
    expect($r->json('balance_rub'))->toBe('3500.00');
    expect(DB::table('tenants')->where('id', $id)->value('balance_rub'))->toBe('3500.00');
    $tx = BalanceTransaction::where('tenant_id', $id)->where('type', 'refund')->first();
    expect($tx)->not->toBeNull();
    expect((string) $tx->amount_rub)->toBe('-1500.00');
    expect((string) $tx->balance_rub_after)->toBe('3500.00');
    expect(DB::table('saas_admin_audit_log')->where('action', 'tenant.refund')->where('target_id', $id)->exists())->toBeTrue();
});

test('POST refund больше баланса → 422, баланс не меняется', function () {
    $id = makeBillingTenant(['balance_rub' => '1000.00']);
    $this->postJson("/api/admin/billing/tenants/{$id}/refund", [
        'amount_rub' => 5000, 'reason' => 'Возврат по обращению клиента №7.',
    ])->assertStatus(422);
    expect(DB::table('tenants')->where('id', $id)->value('balance_rub'))->toBe('1000.00');
    expect(BalanceTransaction::where('tenant_id', $id)->count())->toBe(0);
});

test('POST refund неположительная сумма → 422', function () {
    $id = makeBillingTenant();
    $this->postJson("/api/admin/billing/tenants/{$id}/refund", ['amount_rub' => 0, 'reason' => 'Основание длиннее десяти символов.'])
        ->assertStatus(422);
});

test('PATCH tariff меняет current_tariff_id + audit-log', function () {
    $id = makeBillingTenant();
    $tariffId = makeTariffPlan(['name' => 'Corp Plan', 'price_monthly' => '2500.00']);
    $r = $this->patchJson("/api/admin/billing/tenants/{$id}/tariff", [
        'tariff_id' => $tariffId, 'reason' => 'Переход на тариф по договорённости с клиентом.',
    ]);
    $r->assertOk();
    expect((int) DB::table('tenants')->where('id', $id)->value('current_tariff_id'))->toBe($tariffId);
    expect(DB::table('saas_admin_audit_log')->where('action', 'tenant.change_tariff')->where('target_id', $id)->exists())->toBeTrue();
});

test('PATCH tariff несуществующий tariff_id → 422', function () {
    $id = makeBillingTenant();
    $this->patchJson("/api/admin/billing/tenants/{$id}/tariff", [
        'tariff_id' => 88888888, 'reason' => 'Основание длиннее десяти символов.',
    ])->assertStatus(422);
});
