<?php

namespace App\Http\Controllers\Customer;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class DashboardController extends Controller
{
    public function index()
    {
        $user = Auth::user()->load(['licenses.product', 'orders']);

        $expiryDays = (int) setting('expiry_warning_days', 30);

        $stats = [
            'active_licenses'  => $user->licenses->where('status', 'active')->count(),
            'total_orders'     => $user->orders->count(),
            'expiring_soon'    => $user->licenses->filter(fn($l) => $l->expires_at && $l->expires_at->diffInDays() <= $expiryDays)->count(),
        ];

        $recentLicenses = $user->licenses()->with('product')->latest()->take(5)->get();
        $recentOrders   = $user->orders()->latest()->take(5)->get();

        return view('customer.dashboard', compact('stats', 'recentLicenses', 'recentOrders'));
    }
}
