<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class RequestController extends ApiBaseController
{
    /**
     * Store a newly created stock exchange request in db, after
     * withdrawing money from a user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->all();
        
        $validator = Validator::make($input, array_merge([
            'token' => ['required'],
            'user_balance' => ['required', 'numeric'],
            'phone' => ['required'],
            'first_name' => ['required'],
            'last_name' => ['required'],
            'email' => ['required', 'email'],
            'org_type' => ['required'],
            'items' => ['required', 'array'],
            'items.*.id' => ['required'],
            'totalPrice' => ['required', 'numeric'],
            'currency' => ['required', 'in:TMT,USD']
        ], $this->localeValidationRule()));

        

        if($validator->fails()) {
            return response()->json([
                'status' => 400,
                'validationErrors' => $validator->errors()
            ], 400);
        }


            

                // check user balance
                $fee = $input['totalPrice'];
                
                if($input['user_balance'] - $fee < 0) {
                    return response()->json([
                        'status' => 300, // 300
                        'message' => 'Fill up your balance',
                        'response' => $loginResponse
                    ], 300);
                }

                // try to withdraw fee from the balance for creating a request
                $balanceResponse = Http::post('http://127.0.0.1:8000/api/v1/withdraw-from-balance?token=' . $loginResponse['token'], [
                    // 'token' => $loginResponse['token'],
                    'total_price' => $input['totalPrice'], // USD or TMT
                    'currency' => $input['currency'],
                    'fee' => $fee,
                    'items' => $input['items'],
                    'first_name' => $input['first_name'],
                    'last_name' => $input['last_name'],
                    'org_type' => $input['org_type'],
                    'email' => $input['email']
                ]);
                
                $balanceResponseStatus = $balanceResponse->status();
                
                if($balanceResponseStatus == 201) {
                    RequestModel::create(array_merge($request->only(['first_name', 'last_name', 'email', 'org_type', 'items']), [
                        'phone' => $input['phone']
                    ]));
                }

                if($balanceResponseStatus == 500) {
                    return response()->json([
                        'status' => $balanceResponseStatus, // 500
                        'message' => 'Internal Server Error',
                        'response' => $balanceResponse
                    ], $balanceResponseStatus);
                    break;
                }

                return response()->json([
                    'status' => $balanceResponseStatus, // 201 (ok)
                    'message' => $balanceResponse['message'],
                    'response' => $balanceResponse
                ], $balanceResponseStatus);
                break;
            
            case 500:
                return response()->json([
                    'status' => $loginResponseStatus, // 500
                    'message' => 'Internal Server Error',
                    'response' => $loginResponse
                ], $loginResponseStatus);
                break;

            

    }
}
