<?php

use App\Models\Transaction;
use App\Models\User;

describe('Transactions', function () {

    it('returns user transactions', function () {
        $user = actingAsUser();

        Transaction::factory()->forUser($user)->count(5)->create();

        $response = $this->getJson('/api/v1/transactions');

        $response->assertOk()
            ->assertJsonStructure(['data', 'meta']);
    });

    it('returns specific transaction by reference', function () {
        $user = actingAsUser();

        $transaction = Transaction::factory()->forUser($user)->create();

        $response = $this->getJson("/api/v1/transactions/{$transaction->reference}");

        $response->assertOk()
            ->assertJsonPath('data.reference', $transaction->reference);
    });

    it('cannot view other users transaction', function () {
        $user = actingAsUser();

        $otherUser = User::factory()->withWallet()->create();
        $transaction = Transaction::factory()->forUser($otherUser)->create();

        $response = $this->getJson("/api/v1/transactions/{$transaction->reference}");

        $response->assertStatus(404);
    });
});

describe('Admin', function () {

    it('requires admin access', function () {
        actingAsUser();

        $response = $this->getJson('/api/v1/admin/dashboard');

        $response->assertStatus(403);
    });

    it('allows admin to access dashboard', function () {
        actingAsAdmin();

        $response = $this->getJson('/api/v1/admin/dashboard');

        $response->assertOk()
            ->assertJsonStructure(['data' => ['stats', 'recent_transactions', 'recent_users']]);
    });

    it('allows admin to list users', function () {
        actingAsAdmin();

        $response = $this->getJson('/api/v1/admin/users');

        $response->assertOk();
    });
});
