<?php

namespace App\Http\Controllers\Shop;

use App\Http\Controllers\Controller;
use App\Models\AirtimeToCashRequest;
use App\Services\AirtimeToCashService;
use App\Services\SmeplugBankPayoutService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class AirtimeToCashController extends Controller
{
    public function index(
        Request $request,
        AirtimeToCashService $service,
        SmeplugBankPayoutService $smeplugBankPayoutService
    ): Response
    {
        $user = $request->user();

        $requests = AirtimeToCashRequest::query()
            ->where('user_id', $user->id)
            ->latest()
            ->paginate(20)
            ->withQueryString();

        return Inertia::render('App/AirtimeToCash/Index', [
            'settings' => $service->settings(),
            'requests' => $requests,
            'bankLookup' => $smeplugBankPayoutService->bankLookupStatus(),
        ]);
    }

    public function store(Request $request, AirtimeToCashService $service): RedirectResponse
    {
        $data = $request->validate([
            'network_slug' => ['required', 'string', 'max:30'],
            'sender_phone' => ['required', 'string', 'min:10', 'max:20'],
            'payout_destination' => ['required', 'in:wallet,bank'],
            'bank_name' => ['nullable', 'string', 'max:120', 'required_if:payout_destination,bank'],
            'bank_code' => ['nullable', 'string', 'max:30'],
            'bank_account_number' => ['nullable', 'string', 'min:10', 'max:20', 'required_if:payout_destination,bank'],
            'bank_account_name' => ['nullable', 'string', 'max:191', 'required_if:payout_destination,bank'],
            'airtime_amount' => ['required', 'numeric', 'min:1'],
            'note' => ['nullable', 'string', 'max:1000'],
        ]);

        $service->submit($request->user(), $data);

        return back()->with('success', 'Airtime-to-cash request submitted. Awaiting admin confirmation.');
    }

    public function cancel(Request $request, AirtimeToCashRequest $airtimeToCashRequest, AirtimeToCashService $service): RedirectResponse
    {
        $service->cancel($airtimeToCashRequest, $request->user());

        return back()->with('success', 'Airtime-to-cash request cancelled.');
    }

    public function banks(Request $request, SmeplugBankPayoutService $smeplugBankPayoutService): JsonResponse
    {
        $result = $smeplugBankPayoutService->listBanks();

        return response()->json(
            $result,
            ($result['success'] ?? false) ? 200 : 422
        );
    }

    public function resolveAccount(Request $request, SmeplugBankPayoutService $smeplugBankPayoutService): JsonResponse
    {
        $data = $request->validate([
            'bank_code' => ['required', 'string', 'max:30'],
            'account_number' => ['required', 'string', 'min:10', 'max:20'],
        ]);

        $result = $smeplugBankPayoutService->resolveAccountName(
            bankCode: (string) $data['bank_code'],
            accountNumber: (string) $data['account_number']
        );

        return response()->json(
            $result,
            ($result['success'] ?? false) ? 200 : 422
        );
    }
}
