<?php

namespace App\Http\Controllers\Api\Customer;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\WalletTransaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;

class WalletController extends Controller
{
    /** GET /api/customer/wallet — balance + recent ledger (refunds, etc.). */
    public function index(): JsonResponse
    {
        $user = Auth::guard('customer')->user();

        $recent = WalletTransaction::where('owner_type', User::class)
            ->where('owner_id', $user->id)
            ->latest()
            ->limit(20)
            ->get()
            ->map(fn ($t) => [
                'id'          => $t->id,
                'type'        => $t->type,
                'purpose'     => $t->purpose,
                'amount'      => (float) $t->amount,
                'description' => $t->description,
                'reference'   => $t->reference,
                'created_at'  => optional($t->created_at)->toIso8601String(),
            ]);

        return response()->json([
            'wallet_balance' => (float) $user->wallet_balance,
            'recent'         => $recent,
        ]);
    }
}
