<?php

namespace App\Http\Controllers;

use App\Models\Paypal;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class PaypalController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse
     */
    public function index(): JsonResponse
    {
       $paypal = Paypal::where('provider_id', auth()->user()->id)->first();
       return response()->json($paypal);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function store(Request $request): JsonResponse
    {
        $this->validate($request, [
            'company_name' => 'required|max:255',
            'email' => 'required|max:255',
            'client_id' => 'required|max:255',
        ]);
        $payPal_exist = Paypal::where('provider_id', auth()->user()->id)->first();
        if ($payPal_exist){//update
            $payPal_exist->update($request->all());
        }else{//create
            $data = $request->all();
            $data['provider_id'] = auth()->user()->id;
            Paypal::create($data);
        }
        return response()->json(['msg'=>'PayPal Credential Updated Success']);
    }






}
