<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\Gateway;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class PaymentGatewayController extends Controller
{
    public function paystack()
    {
        $title = 'Paystack';
        $gateway = Gateway::where('gateway_name', 'paystack')->firstOrNew([]);
        return view('backend.gateways.paystack', compact('title', 'gateway'));
    }

    public function paystackUpdate(Request $request)
    {
        $gateway = Gateway::where('gateway_name', 'paystack')->first();

        $request->validate([
            'gateway_currency' => 'required|string|max:10',
            'paystack_public_key' => 'required|string|max:255',
            'paystack_key' => 'required|string|max:255',
            'rate' => 'required|numeric|min:0',
            'status' => 'required|in:0,1',
            'paystack_image' => [Rule::requiredIf(!$gateway), 'nullable', 'image', 'mimes:jpeg,png,jpg,gif', 'max:2048'],
        ]);

        $params = [
            'gateway_currency' => $request->gateway_currency,
            'paystack_public_key' => $request->paystack_public_key,
            'paystack_key' => $request->paystack_key,
        ];

        $filename = $gateway->gateway_image ?? null;
        if ($request->hasFile('paystack_image')) {
            if ($gateway && $gateway->gateway_image && file_exists(base_path('../'.$gateway->gateway_image))) {
                unlink(base_path('../'.$gateway->gateway_image));
            }
            $file = $request->file('paystack_image');
            $filename = 'assets/backend/images/gateways/' . time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $dest = base_path('../assets/backend/images/gateways');
            if (!file_exists($dest)) {
                mkdir($dest, 0755, true);
            }
            $file->move($dest, basename($filename));
        }

        Gateway::updateOrCreate(
            ['gateway_name' => 'paystack'],
            [
                'gateway_image' => $filename ?? $gateway->gateway_image ?? null,
                'gateway_parameters' => $params,
                'gateway_type' => 1,
                'rate' => $request->rate,
                'status' => (int) $request->status,
            ]
        );

        return redirect()->back()->with('success', 'Paystack settings updated successfully');
    }

    public function flutterwave()
    {
        $title = 'Flutterwave';
        $gateway = Gateway::where('gateway_name', 'flutterwave')->firstOrNew([]);
        return view('backend.gateways.flutterwave', compact('title', 'gateway'));
    }

    public function flutterwaveUpdate(Request $request)
    {
        $gateway = Gateway::where('gateway_name', 'flutterwave')->first();

        $request->validate([
            'gateway_currency' => 'required|string|max:10',
            'public_key' => 'required|string|max:255',
            'secret_key' => 'required|string|max:255',
            'rate' => 'required|numeric|min:0',
            'status' => 'required|in:0,1',
            'flutterwave_image' => [Rule::requiredIf(!$gateway), 'nullable', 'image', 'mimes:jpeg,png,jpg,gif', 'max:2048'],
        ]);

        $params = [
            'gateway_currency' => $request->gateway_currency,
            'public_key' => $request->public_key,
            'secret_key' => $request->secret_key,
        ];

        $filename = $gateway->gateway_image ?? null;
        if ($request->hasFile('flutterwave_image')) {
            if ($gateway && $gateway->gateway_image && file_exists(base_path('../'.$gateway->gateway_image))) {
                unlink(base_path('../'.$gateway->gateway_image));
            }
            $file = $request->file('flutterwave_image');
            $filename = 'assets/backend/images/gateways/' . time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $dest = base_path('../assets/backend/images/gateways');
            if (!file_exists($dest)) {
                mkdir($dest, 0755, true);
            }
            $file->move($dest, basename($filename));
        }

        Gateway::updateOrCreate(
            ['gateway_name' => 'flutterwave'],
            [
                'gateway_image' => $filename ?? $gateway->gateway_image ?? null,
                'gateway_parameters' => $params,
                'gateway_type' => 1,
                'rate' => $request->rate,
                'status' => (int) $request->status,
            ]
        );

        return redirect()->back()->with('success', 'Flutterwave settings updated successfully');
    }

    public function bank()
    {
        $title = 'Bank Transfer';
        $gateway = Gateway::where('gateway_name', 'bank')->firstOrNew([]);
        return view('backend.gateways.bank', compact('title', 'gateway'));
    }

    public function bankUpdate(Request $request)
    {
        $gateway = Gateway::where('gateway_name', 'bank')->first();

        $request->validate([
            'name' => 'required|string|max:255',
            'account_number' => 'required|string|max:100',
            'routing_number' => 'required|string|max:100',
            'branch_name' => 'required|string|max:255',
            'gateway_currency' => 'required|string|max:10',
            'rate' => 'required|numeric|min:0',
            'charge' => 'required|numeric|min:0',
            'status' => 'required|in:0,1',
            'bank_image' => [Rule::requiredIf(!$gateway), 'nullable', 'image', 'mimes:jpeg,png,jpg,gif', 'max:2048'],
        ]);

        $params = [
            'name' => $request->name,
            'account_number' => $request->account_number,
            'routing_number' => $request->routing_number,
            'branch_name' => $request->branch_name,
            'gateway_currency' => $request->gateway_currency,
        ];

        $filename = $gateway->gateway_image ?? null;
        if ($request->hasFile('bank_image')) {
            if ($gateway && $gateway->gateway_image && file_exists(base_path('../'.$gateway->gateway_image))) {
                unlink(base_path('../'.$gateway->gateway_image));
            }
            $file = $request->file('bank_image');
            $filename = 'assets/backend/images/gateways/' . time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
            $dest = base_path('../assets/backend/images/gateways');
            if (!file_exists($dest)) {
                mkdir($dest, 0755, true);
            }
            $file->move($dest, basename($filename));
        }

        Gateway::updateOrCreate(
            ['gateway_name' => 'bank'],
            [
                'gateway_image' => $filename ?? $gateway->gateway_image ?? null,
                'gateway_parameters' => $params,
                'gateway_type' => 0,
                'rate' => $request->rate,
                'charge' => $request->charge,
                'status' => (int) $request->status,
            ]
        );

        return redirect()->back()->with('success', 'Bank transfer settings updated successfully');
    }
}