<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\PartnerWalletTopupRequest;
use App\Models\PartnerWallet;
use App\Services\PartnerWalletService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

/**
 * إدارة طلبات شحن المحفظة (المطعم / مكتب الغاز)
 * الإدارة تؤكد كود التحويل والرقم ثم تفعّل الشحن → يُضاف المبلغ للمحفظة
 */
class PartnerWalletTopupController extends Controller
{
    public function index(Request $request)
    {
        $status = $request->get('status', 'pending'); // pending | approved | rejected
        $query = PartnerWalletTopupRequest::with('partnerWallet.owner')->orderBy('created_at', 'desc');
        if (in_array($status, ['pending', 'approved', 'rejected'])) {
            $query->where('status', $status);
        }
        $requests = $query->paginate(20);

        return view('admin.wallet-topup.index', compact('requests', 'status'));
    }

    public function approve(int $id)
    {
        $topupRequest = PartnerWalletTopupRequest::with('partnerWallet')->findOrFail($id);
        if ($topupRequest->status !== PartnerWalletTopupRequest::STATUS_PENDING) {
            return redirect()->route('admin.wallet-topup.index')
                ->with('error', 'هذا الطلب تمت معالجته مسبقاً');
        }

        DB::beginTransaction();
        try {
            PartnerWalletService::topUp(
                $topupRequest->partner_wallet_id,
                (float) $topupRequest->amount,
                $topupRequest->payment_method,
                'topup_request_' . $topupRequest->id,
                'شحن محفظة - تم التأكيد من الإدارة'
            );

            $topupRequest->update([
                'status' => PartnerWalletTopupRequest::STATUS_APPROVED,
                'reviewed_by' => Auth::id(),
                'reviewed_at' => now(),
            ]);

            DB::commit();
            return redirect()->route('admin.wallet-topup.index')
                ->with('success', 'تم تفعيل الشحن وإضافة المبلغ للمحفظة بنجاح');
        } catch (\Exception $e) {
            DB::rollBack();
            return redirect()->route('admin.wallet-topup.index')
                ->with('error', 'فشل في تفعيل الشحن: ' . $e->getMessage());
        }
    }

    public function reject(Request $request, int $id)
    {
        $topupRequest = PartnerWalletTopupRequest::findOrFail($id);
        if ($topupRequest->status !== PartnerWalletTopupRequest::STATUS_PENDING) {
            return redirect()->route('admin.wallet-topup.index')
                ->with('error', 'هذا الطلب تمت معالجته مسبقاً');
        }

        $topupRequest->update([
            'status' => PartnerWalletTopupRequest::STATUS_REJECTED,
            'admin_notes' => $request->input('admin_notes'),
            'reviewed_by' => Auth::id(),
            'reviewed_at' => now(),
        ]);

        return redirect()->route('admin.wallet-topup.index')
            ->with('success', 'تم رفض طلب الشحن');
    }
}
