<?php

use App\Models\User;
use Illuminate\Support\Facades\Http;

describe('Authentication', function () {

    it('allows user to register', function () {
        $response = $this->postJson('/api/v1/register', [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => 'john@example.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
            'terms' => true,
        ]);

        $response->assertStatus(201)
            ->assertJsonStructure([
                'success',
                'message',
                'data' => ['user', 'token'],
            ]);

        $this->assertDatabaseHas('users', ['email' => 'john@example.com']);
    });

    it('validates registration fields', function () {
        $response = $this->postJson('/api/v1/register', []);

        $response->assertStatus(422)
            ->assertJsonValidationErrors(['first_name', 'last_name', 'email', 'password', 'terms']);
    });

    it('prevents duplicate email registration', function () {
        User::factory()->create(['email' => 'john@example.com']);

        $response = $this->postJson('/api/v1/register', [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => 'john@example.com',
            'password' => 'password123',
            'password_confirmation' => 'password123',
            'terms' => true,
        ]);

        $response->assertStatus(422)
            ->assertJsonValidationErrors(['email']);
    });

    it('allows user to login', function () {
        User::factory()->create([
            'email' => 'john@example.com',
            'password' => 'password123',
        ]);

        $response = $this->postJson('/api/v1/login', [
            'email' => 'john@example.com',
            'password' => 'password123',
            'device_name' => 'Test Device',
        ]);

        $response->assertOk()
            ->assertJsonStructure([
                'success',
                'data' => ['user', 'token'],
            ]);
    });

    it('rejects invalid login credentials', function () {
        $response = $this->postJson('/api/v1/login', [
            'email' => 'john@example.com',
            'password' => 'wrongpassword',
            'device_name' => 'Test Device',
        ]);

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

    it('allows user to logout', function () {
        $user = actingAsUser();

        $response = $this->postJson('/api/v1/logout');

        $response->assertOk();
    });

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

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

        $response->assertOk()
            ->assertJsonPath('data.user.id', $user->id);
    });
});

describe('Wallet', function () {

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

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

        $response->assertOk()
            ->assertJsonStructure(['data' => ['wallet']]);
    });

    it('can fund wallet', function () {
        $user = actingAsUser();

        Http::fake([
            config('services.paystack.base_url') . '/transaction/initialize' => Http::response([
                'status' => true,
                'data' => [
                    'authorization_url' => 'https://checkout.paystack.com/test',
                    'reference' => 'test-ref',
                    'amount' => 500000,
                ],
            ], 200),
        ]);

        $response = $this->postJson('/api/v1/wallet/fund', [
            'amount' => 5000,
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('payments', [
            'user_id' => $user->id,
            'amount' => 5000,
            'status' => 'pending',
        ]);
    });
});

describe('Services', function () {

    it('returns active bill services', function () {
        actingAsUser();

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

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