<?php

namespace App\Http\Controllers\Customer;

use App\Http\Controllers\Controller;
use App\Models\Bid;
use App\Models\Order;
use App\Models\Watchlist;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        $customer = $user->customer;

        $activeBids = Bid::with('auctionVehicle.auctionHouse')
            ->where('user_id', $user->id)
            ->whereIn('status', ['pending', 'approved', 'submitted', 'bidding'])
            ->orderBy('created_at', 'desc')
            ->get();

        $recentOrders = Order::with('vehicle', 'auctionVehicle', 'statusHistory')
            ->where('user_id', $user->id)
            ->orderBy('created_at', 'desc')
            ->take(5)
            ->get();

        $watchlistCount = Watchlist::where('user_id', $user->id)->count();

        $wonAuctionsCount = Bid::where('user_id', $user->id)->where('status', 'won')->count();

        $notifications = $user->notifications()->take(5)->get();

        return view('customer.dashboard', compact(
            'user',
            'customer',
            'activeBids',
            'recentOrders',
            'watchlistCount',
            'wonAuctionsCount',
            'notifications'
        ));
    }
}
