<?php

namespace App\Http\Controllers\Mall;

use App\Http\Controllers\Controller;
use App\Models\Mall\MallStorage;
use App\Models\Mall\MallHistory;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class UserDataController extends Controller
{
    /**
     * Show user data page
     *
     * @return View
     */
    public function index(): View
    {
        $user = Auth::user();

        // Get count of unique items in storage
        try {
            $storageCount = MallStorage::getUniqueItemCount($user->id);
        } catch (\Exception $e) {
            \Log::error('MallStorage error: ' . $e->getMessage());
            $storageCount = 0;
        }

        // Get count of purchases in the last 30 days
        try {
            $boughtCount = MallHistory::where('account_id', $user->id)
                ->where('created_at', '>=', now()->subDays(30))
                ->count();
        } catch (\Exception $e) {
            \Log::error('MallHistory error: ' . $e->getMessage());
            $boughtCount = 0;
        }

        return view('mall/userdata', [
            'storageCount' => $storageCount,
            'boughtCount' => $boughtCount,
        ]);
    }
}
