<?php

namespace App\Http\Controllers;

use App\Models\Loan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'amount' => 'required',
            'description' => 'required',
        ]);

        $loan = new Loan;
        $loan->user_id = Auth::id();
        $loan->amount = $request->amount;
        $loan->status = 'pending';
        $loan->description = $request->description;
        $loan->save();

        return response()->json([
            'status' => 'success',
            'message' => 'your loan request has been submitted successfully and awaiting approval by admin, we will update you via email with the status of your request. Thank you.'
        ], 200);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Loan  $loan
     * @return \Illuminate\Http\Response
     */
    public function show(Loan $loan)
    {
        $loans = Loan::orderByDesc('id')->take(50)->get()->load(['user']);
        return response()->json([
            'status' => 'success',
            'message' => 'loans loaded successfully',
            'loans' => $loans
        ], 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Loan  $loan
     * @return \Illuminate\Http\Response
     */
    public function edit(Loan $loan)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Loan  $loan
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $request->validate([
            'status' => 'required',
            'loan_id' => 'required'
           ]);

        $loan = Loan::find($request->loan_id);
        
        if(!$loan){
            return response()->json([
                'status' => 'error',
                'message'=> 'invalid request'
            ], 400);
        }

        $loan->status = $request->status;
        $loan->created_at =  now();
        $loan->save();

        return response()->json([
            'status' => 'success',
            'message' => 'loan approved successfully',            
        ], 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Loan  $loan
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $loan = Loan::find($id);
        if(!$loan){
            return response()->json([
                'status' => 'error',
                'message'=> 'invalid request'
            ], 400);
        }

        $loan->status = 'declined';
        $loan->save();
        // $countries = new Countries();
        // $user_country = $countries->where('cca2', $loan->user->country)->first()->abbrev;

        //  // notify the user of confirmed deposit
        //  $notification_details = [
        //     'title' => 'Deposit Declined',
        //     'content' => $loan->user->name.' from '.$user_country.' just traded with $'.number_format($loan->amount, 2, '.', ','),
        //     'mail_content' => 'Dear '. $loan->user->name.', this is to notify you that your loan in our '.number_format($loan->amount, 2, '.', ',').' has been declined by our admin, if you have any questions about this action, kindly contact our support for assistance. Thank you.',
        // ];
        // $activity = (object)$notification_details;
        // $loan->user->notify(new GeneralNotification($activity));

        return response()->json([
            'status' => 'success',
            'message' => 'loan declined successfully',
        ], 200);
    }
}
