<?php

namespace App\Http\Controllers;

use App\Models\BankDetail;
use App\Models\User;
use App\Models\Website;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;

class BankCardController extends Controller
{
    public function BankCard()
    {
        $banks = BankDetail::where(['user_id' => Auth::user()->id])->latest()->paginate(10);
        return view('userDashboard.bank-card', compact('banks'));
    }

    public function AddBankCard()
    {
        return view('userDashboard.add-bank-card');
    }

    public function SaveBankCard(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'upi' => 'required',
            'ifsc' => 'required',
            'bank_name' => 'required',
            'account' => 'required',
            'state' => 'required',
            'city' => 'required',
            'address' => 'required',
            'mobile_number' => 'required',
            'email' => 'required',
        ]);
        $bank_account = new BankDetail();
        $bank_account->name = $request->name;
        $bank_account->upi = $request->upi;
        $bank_account->ifsc = $request->ifsc;
        $bank_account->bank_name = $request->bank_name;
        $bank_account->account = $request->account;
        $bank_account->state = $request->state;
        $bank_account->city = $request->city;
        $bank_account->address = $request->address;
        $bank_account->email = $request->email;
        $bank_account->mobile = $request->mobile_number;
        $bank_account->user_id = Auth::user()->id;
        $bank_account->save();
        return redirect('/user/bankCard');
    }

    public function BankMobileSearch(Request $request)
    {
        $request->validate([
            'mobile'    =>  'required|min:10'
        ]);
        $mobile = $request->mobile;
        $otp = rand(1000, 10000);
        $user = User::where(['mobile'   =>  $mobile])->first();
        if ($user) {
            Session::put("bankaccount_otp", $otp);
            $website_settings = Website::first();
            // $message = "$website_settings->title verification code is $otp - $website_settings->title Team";
            $message = $otp;
            if (env('FAST_2_SMS_AUTH') && env('FAST_2_SMS_AUTH') != "") {
                $sms_api_token = env('FAST_2_SMS_AUTH');
            } else {
                $sms_api_token = $website_settings->sms_api;
            }
            $fields = [
                "route" => "otp",
                "variables_values" => $message,
                "language" => "english",
                "flash" => 0,
                "numbers" => $mobile,
            ];
            $http_request = Http::withHeaders([
                "Content-Type" => "application/json",
                "Authorization" => $sms_api_token,
            ])->post("https://www.fast2sms.com/dev/bulkV2", $fields);
            $response = $http_request->json();

            Log::info($response);

            if (!$http_request->ok()) {
                return response()->json(['status'   =>  'error', 'message'    =>  "Something went wrong!"]);
            } elseif (!$response['return']) {
                return response()->json(['status'   =>  'invalid', 'message' => $response['message']]);
            }
            return response()->json(['status'   =>  'success', 'message'    =>  $response, 'otp'    =>  $otp]);
        } else {
            return response()->json(['status'   =>  'error']);
        }
    }

    public function CheckBankAccountOtp(Request $request)
    {
        $request->validate([
            'value' =>  'required'
        ]);
        if (Session::has('bankaccount_otp') && Session::get('bankaccount_otp') == $request->value) {
            return response()->json(['status'   =>  'success']);
        } else {
            return response()->json(['status'   =>  'error']);
        }
    }

    public function EditBankCard($id)
    {
        $card = BankDetail::where(['id' => $id, 'user_id' => Auth::user()->id])->first();
        if ($card) {
            return view('userDashboard.edit-bank-card', compact('card'));
        } else {
            return redirect()->back();
        }
    }

    public function UpdateBankCard(Request $request)
    {
        $request->validate([
            'id' => 'required',
            'name' => 'required',
            'upi' => 'required',
            'ifsc' => 'required',
            'bank_name' => 'required',
            'account' => 'required',
            'state' => 'required',
            'city' => 'required',
            'address' => 'required',
            'mobile_number' => 'required',
            'email' => 'required'
        ]);
        BankDetail::where([
            'id' => $request->id,
            'user_id' => Auth::user()->id
        ])->update([
            'name' => $request->name,
            'upi' => $request->upi,
            'ifsc' => $request->ifsc,
            'bank_name' => $request->bank_name,
            'account' => $request->account,
            'state' => $request->state,
            'city' => $request->city,
            'address' => $request->address,
            'email' => $request->email,
            'mobile' => $request->mobile_number
        ]);
        return redirect('/user/bankCard');
    }

    public function DeleteBankCard($id)
    {
        BankDetail::where(['id' => $id, 'user_id' => Auth::user()->id])->delete();
        return redirect()->back();
    }
}
