<?php

namespace App\Livewire\Admin;

use App\Models\Order;
use App\Models\Partner;
use App\Models\Product;
use App\Models\User;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.admin')]
#[Title('Dashboard')]
class Dashboard extends Component
{
    public function render()
    {
        // Stats — menggunakan data nyata dari database
        $stats = [
            'total_produk'   => Product::where('is_active', true)->count(),
            'total_pesanan'  => Order::count(),
            'total_customer' => User::role('customer')->count(),
            'total_mitra'    => Partner::where('is_active', true)->count(),
            'pendapatan_hari_ini' => Order::whereDate('created_at', today())
                ->whereIn('status', ['processing', 'delivering', 'completed'])
                ->sum('grand_total'),
        ];

        // 5 pesanan terbaru
        $recentOrders = Order::with(['items'])
            ->latest()
            ->limit(5)
            ->get();

        return view('livewire.admin.dashboard', compact('stats', 'recentOrders'));
    }
}
