<?php

namespace App\Http\Controllers;
use App\Interfaces\UserRepositoryInterface;
use Illuminate\Http\Request;
use App\Models\BankInfo;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;



class UserBankController extends Controller
{
    private UserRepositoryInterface $userRepository;

    public function __construct( UserRepositoryInterface $userRepository)
    {
        $this->middleware('auth:api');
        $this->userRepository = $userRepository;
    }


     //Get Profile
        public function index(){

            try {
            $user = Auth::user();
            $profile = $this->userRepository->getBankProfile($user->id);
            
            return response()->json([
                'status' => 'success',
                'message' => 'Bank profile returned successfully',
                'data' => $profile
            ]);
            }  catch (\Exception $e) {
            return response()->json([
                'status' =>'failed',
                'message'=> $e->getMessage(),
            ], 500);
        }
        }

        //Update Profile
    public function updateProfile(Request $request)
    {
        try {

        // Define validation rules
        $validator = Validator::make($request->all(), [
            'account_number' => 'string|min:10',
                'bvn' => 'nullable|string|min:11',
            ]);

        // Check if validation fails
        if ($validator->fails()) {
            return response()->json([
                'status' => 'failed',
                'message' => $validator->errors()->first(),
            ], 422);
        }
            $id = $request->id;

            $profile = $this->userRepository->updateBankProfile($id,$request->all());
            return response()->json([
                'status' => 'success',
                'message' => 'Bank profile updated successfully',
                'data' => $profile
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'status' =>'failed',
                'message'=> $e->getMessage(),
            ], 500);
        }
    }
}
