<?php

namespace App\Http\Controllers;

use App\Models\CreditHistory;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class CreditHistoryController extends Controller
{
    /**
     * Riwayat mutasi credit (FR-CRED-02). Admin lihat semua, member lihat miliknya.
     */
    public function index(Request $request): Response
    {
        $user = $request->user();

        $histories = CreditHistory::with('user')
            ->when(! $user->isAdmin(), fn ($q) => $q->where('user_id', $user->id))
            ->latest('tanggal')
            ->paginate(20)
            ->withQueryString();

        return Inertia::render('credits/Index', [
            'histories' => $histories,
            'credit' => $user->credit,
        ]);
    }
}
