<?php

namespace App\Http\Controllers\Web;

use Inertia\Inertia;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\RequestResource;
use App\Models\Request as RequestModel;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use TimeHunter\LaravelGoogleReCaptchaV3\Validations\GoogleReCaptchaV3ValidationRule;

class RequestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $requests = RequestModel::query()
            ->orderByDesc('created_at')
            ->get();

        return Inertia::render('Requests', [
            'requests' => RequestResource::collection($requests),
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->all();
        $rules = [
            'name' => ['required'],
            'phone' => ['required'],
            'email' => ['required', 'email'],
            'password' => ['required'],
            'items' => ['required', 'array'],
            'items.*.id' => ['required'],
        ];
        $validator = Validator::make($input, $rules);
        if($validator->fails()) {
            return response()->json([
                'status' => 400,
                'validationErrors' => $validator->errors()
            ], 400);
        }

        $loginResponse = Http::post('http://127.0.0.1:8000/api/login', [
            'username' => $input['phone'],
            'password' => $input['password']
        ]);

        $loginResponseStatus = $loginResponse->status();

        switch ($loginResponseStatus) {
            case 401:
                return response()->json([
                    'status' => $loginResponseStatus, // 401
                    'message' => 'Unauthorized',
                    'response' => $loginResponse
                ], $loginResponseStatus);
                // break;

            case 200:
                // try to withdraw from the balance
                $balanceResponse = Http::post('http://127.0.0.1:8000/api/v1/withdraw-from-balance?token=' . $loginResponse['token']);
                
                $balanceResponseStatus = $balanceResponse->status();
                
                if($balanceResponseStatus == 201) {
                    RequestModel::create($input);
                }

                return response()->json([
                    'status' => $balanceResponseStatus, // 201 (ok) or 300 (fill up balance)
                    'message' => $balanceResponse['message'],
                    'response' => $balanceResponse
                ], $balanceResponseStatus);
                // break; not needed because of return
            
            case 500:
                return response()->json([
                    'status' => $loginResponseStatus, // 500
                    'message' => 'Internal Server Error',
                    'response' => $loginResponse
                ], $loginResponseStatus);
                // break;

            default:
                return response()->json([
                    'status' => $loginResponseStatus,
                    'message' => 'Unknown Server Error',
                    'response' => $loginResponse
                ], $loginResponseStatus);
                // break;
        }

        // RequestModel::create($form);

        // return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function destroy(RequestModel $request)
    {
        $request->delete();

        return redirect()->back();
    }
}
