<?php

namespace App\Http\Controllers\MerchantAdmin;

use App\Http\Controllers\Concerns\ResolvesMerchantContext;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Support\CacheKeys;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;

class UserController extends Controller
{
    use ResolvesMerchantContext;

    public function index(Request $request): View
    {
        $merchantId = $this->merchantId($request);

        $page = max((int) $request->integer('page', 1), 1);

        return view('merchant_admin.users.index', [
            'users' => Cache::remember(
                CacheKeys::merchantUsersList($merchantId, $page),
                now()->addMinutes(10),
                fn () => User::query()->forMerchant($merchantId)->latest()->paginate(10)->withQueryString()
            ),
            'cacheMeta' => [
                'store' => config('cache.default'),
                'ttl' => '10m',
            ],
        ]);
    }
}
