<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Transaction;
use App\Modules\Wallet\Interfaces\WalletServiceInterface;
use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index(Request $request)
    {
        $user = $request->user();
        $walletService = app(WalletServiceInterface::class);

        $wallet = $walletService->getOrCreateWallet($user->id);
        $balance = $walletService->getBalance($user->id);
        $recentTransactions = $walletService->getTransactions($user->id, 5);

        $stats = [
            'balance' => $balance,
            'total_funded' => $wallet->total_funded,
            'total_spent' => $wallet->total_spent,
            'successful_transactions' => $user->transactions()->where('status', 'successful')->count(),
        ];

        return view('dashboard.index', compact('stats', 'recentTransactions', 'wallet'));
    }
}
