<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\Finance\AccountTransaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AccountTransactionController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = AccountTransaction::query()->with(['account', 'customer']);
        $query->when($request->customer_account_id, fn($q, $v) => $q->where('customer_account_id', $v));
        $query->when($request->customer_id, fn($q, $v) => $q->where('customer_id', $v));
        $query->when($request->type, fn($q, $v) => $q->where('type', $v));

        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }
}
