<?php

use App\Models\PaymentEvent;
use App\Models\User;
use App\Services\Payments\PaymentLogger;

test('can log payment event', function () {
    $logger = new PaymentLogger;

    $logger->logEvent('test_subscription_created', [
        'subscription_id' => 123,
        'provider' => 'stripe',
        'amount' => 10.00,
    ]);

    $event = PaymentEvent::where('event_type', 'test_subscription_created')->first();

    expect($event)->not->toBeNull();
    expect($event->level)->toBe('info');
    expect($event->data['subscription_id'])->toBe(123);
    expect($event->data['provider'])->toBe('stripe');
    expect($event->data['amount'])->toBe(10.00);
});

test('can log error event', function () {
    $logger = new PaymentLogger;

    $logger->logError('test_payment_failed', [
        'subscription_id' => 456,
        'error' => 'Payment declined',
    ]);

    $event = PaymentEvent::where('event_type', 'test_payment_failed')->first();

    expect($event)->not->toBeNull();
    expect($event->level)->toBe('error');
    expect($event->data['error'])->toBe('Payment declined');
});

test('can log security event', function () {
    $logger = new PaymentLogger;

    $logger->logSecurityEvent('suspicious_activity', [
        'ip_address' => '192.168.1.1',
        'user_agent' => 'Test Agent',
    ]);

    $event = PaymentEvent::where('event_type', 'security_suspicious_activity')->first();

    expect($event)->not->toBeNull();
    expect($event->level)->toBe('warning');
    expect($event->data['requires_review'])->toBeTrue();
});

test('can get user audit trail', function () {
    $user = User::factory()->create();
    $logger = new PaymentLogger;

    // Log some events for the user
    $logger->logEvent('test_event_1', ['user_id' => $user->id]);
    $logger->logEvent('test_event_2', ['user_id' => $user->id]);

    $trail = $logger->getUserAuditTrail($user->id);

    expect($trail)->toHaveCount(2);
    expect($trail[0]['event_type'])->toBe('test_event_2'); // Latest first
    expect($trail[1]['event_type'])->toBe('test_event_1');
});

test('can generate compliance report', function () {
    $logger = new PaymentLogger;

    // Log some events
    $logger->logEvent('compliance_data_access', ['user_id' => 1]);
    $logger->logEvent('subscription_created', ['user_id' => 2]);
    $logger->logError('payment_failed', ['user_id' => 3]);

    $report = $logger->generateComplianceReport();

    expect($report['total_events'])->toBe(3);
    expect($report['events_by_type']['compliance_data_access'])->toBe(1);
    expect($report['events_by_type']['subscription_created'])->toBe(1);
    expect($report['events_by_type']['payment_failed'])->toBe(1);
    expect($report['events_by_level']['info'])->toBe(2);
    expect($report['events_by_level']['error'])->toBe(1);
});
