<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\DepositAddress;
use App\Models\Deposit;

class DepositController extends Controller
{
    public function index(Request $request)
    {
        return view('admin.deposits', [
            'page'      => 'Deposits'
        ]);
    }
    public function fetchHistory(Request $request, $status)
    {
        if (!$request->ajax()) {
            return response()->json(['error' => 'Invalid request'], 400);
        }
        $perPage = 10;

        $Deposit = Deposit::with('user') 
            ->orderBy('updated_at', 'desc')
            ->where('status', $status)
            ->paginate($perPage);
            
        $records = $Deposit->map(function ($item) {
            return [
                'user_id' => $item->user_id,
                'username' => optional($item->user)->name,
                'method' => $item->method,
                'status' => $this->getStatusLabel($item->status),
                'amount' => $item->amount,
                'txn_id' => $item->txn_id,
                'date' => $item->updated_at->diffForHumans(),
            ];
        });
    
        return response()->json([
            'records' => $records,
            'current_page' => $Deposit->currentPage(),
            'total_pages' => $Deposit->lastPage(),
            'prev_page' => $Deposit->previousPageUrl() ? $Deposit->currentPage() - 1 : null,
            'next_page' => $Deposit->nextPageUrl() ? $Deposit->currentPage() + 1 : null,
        ]);
    }
    public function fetchUserHistory(Request $request)
    {
        if (!$request->ajax()) {
            return response()->json(['error' => 'Invalid request'], 400);
        }
        $perPage = 10;
        $uid = $request->input('uid');
        $Deposit = Deposit::orderBy('updated_at', 'desc')
            ->where('user_id', $uid)
            ->paginate($perPage);
            
        $records = $Deposit->map(function ($item) {
            return [
                'user_id' => $item->user_id,
                'method' => $item->method,
                'status' => $this->getStatusLabel($item->status),
                'amount' => $item->amount,
                'txn_id' => $item->txn_id,
                'date' => $item->updated_at->diffForHumans(),
            ];
        });
    
        return response()->json([
            'records' => $records,
            'current_page' => $Deposit->currentPage(),
            'total_pages' => $Deposit->lastPage(),
            'prev_page' => $Deposit->previousPageUrl() ? $Deposit->currentPage() - 1 : null,
            'next_page' => $Deposit->nextPageUrl() ? $Deposit->currentPage() + 1 : null,
        ]);
    }
    private function getStatusLabel($status)
    {
        $statusLabels = [
            'pending' => ['text' => 'Pending', 'color' => 'warning'],
            'completed' => ['text' => 'Completed', 'color' => 'success'],
            'failed' => ['text' => 'Failed', 'color' => 'error'],
        ];
    
        return $statusLabels[$status] ?? ['text' => 'Unknown', 'color' => 'secondary'];
    }
}
