<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ShopUser;
use Inertia\Inertia;

class AdminShopUserController extends Controller
{
    public function index()
    {
        $shopUsers = ShopUser::with(['unlockOrders', 'payments'])
            ->latest()
            ->get()
            ->map(function ($user) {
                return [
                    'id' => $user->id,
                    'shop_name' => $user->shop_name,
                    'phone_number' => $user->phone_number,
                    'total_orders' => $user->unlockOrders()->count(),
                    'completed_orders' => $user->unlockOrders()->where('status', 'done')->count(),
                    'balance' => $user->getBalance(),
                    'created_at' => $user->created_at,
                ];
            });

        return Inertia::render('Admin/Pages/ShopUsers/Index', [
            'shopUsers' => $shopUsers,
        ]);
    }

    public function show(ShopUser $shopUser)
    {
        $shopUser->load(['unlockOrders', 'payments']);

        return Inertia::render('Admin/Pages/ShopUsers/Show', [
            'shopUser' => $shopUser,
        ]);
    }
}
