<?php

namespace App\Http\Controllers\Web\Admin;

use App\Http\Controllers\Controller;
use App\Models\Transaction;
use App\Models\User;
use App\Models\Payment;
use Illuminate\Http\Request;

class AdminDashboardController extends Controller
{
    public function index()
    {
        $stats = [
            'total_users' => User::count(),
            'active_users' => User::where('status', 'active')->count(),
            'new_users_today' => User::whereDate('created_at', today())->count(),
            'total_transactions' => Transaction::count(),
            'successful_transactions' => Transaction::where('status', 'successful')->count(),
            'failed_transactions' => Transaction::where('status', 'failed')->count(),
            'total_revenue' => Transaction::where('status', 'successful')->sum('fee'),
            'today_revenue' => Transaction::where('status', 'successful')->whereDate('created_at', today())->sum('fee'),
            'total_volume' => Transaction::where('status', 'successful')->sum('total_amount'),
            'pending_transactions' => Transaction::where('status', 'pending')->count(),
            'total_funded' => Payment::where('status', 'successful')->sum('amount'),
        ];

        $recentTransactions = Transaction::with('user')->orderBy('created_at', 'desc')->limit(5)->get();
        $recentUsers = User::orderBy('created_at', 'desc')->limit(5)->get();

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