<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Transaction;

class HistoryTransactionController extends Controller
{
    public function index(Request $request): View
    {
        $query = Transaction::where('user_id', Auth::id())->latest();
    
        if ($request->filled('search')) {
            $search = $request->search;
            $query->where(function ($q) use ($search) {
                $q->where('trx_id', 'like', "%$search%")
                  ->orWhere('target_id', 'like', "%$search%");
            });
        }
    
        if ($request->filled('trx_status')) {
            $query->where('trx_status', $request->trx_status);
        }
    
        if ($request->filled('date')) {
            $query->whereDate('created_at', $request->date);
        }
    
        if ($request->filled('month')) {
            $query->whereMonth('created_at', $request->month);
        }
    
        if ($request->filled('year')) {
            $query->whereYear('created_at', $request->year);
        }
    
        $transactions = $query->paginate(10)->withQueryString();
    
        return view('pages.user.transactions.history', compact('transactions'));
    }
}
