<?php

namespace App\Http\Controllers\API;

# Models
use App\Models\Bit;
use App\Models\BeadOnMotor;

# Other Classes
use DB;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use App\Http\Controllers\API\BaseController as BaseController;

class BeadOnMotorController extends BaseController
{

     #Bind Bit model
    protected $bit;

     #Bind BeadOnMotor model
    protected $beadOnMotor;


    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                Bit $bit,
                                BeadOnMotor $beadOnMotor
                            )
    {
        $this->bit  = $bit;
        $this->beadOnMotor       = $beadOnMotor;
        
    }

    /**
     * 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)
    {
        DB::beginTransaction();
        try{
            $validator = Validator::make($request->all(), [
                'stores_id' => 'required',
                'game_id' => 'required',
                'type' => 'required',
                'amount' => 'required',
                'bit_place' => 'required',
            ]);

            if($validator->fails()){
                return $this->sendError('Validation Error.', $validator->errors());       
            }
            $totalamount = totalAmount(auth('api')->user()->id);
            if($request->amount>$totalamount){
                return $this->sendError('Error.', "You have not sufficient balance");    
            }else{
                debitAmount(auth('api')->user()->id,$request->amount);
            }
                $bitcheck = $this->bit->where('created_at','like',currentDate().'%')->where('stores_id',$request->stores_id)->where('game_id',$request->game_id)->where('day_id',$request->day_id)->where('user_id',auth('api')->user()->id)->first();
                if(!empty($bitcheck)){
                    $bitcheck->amount = $bitcheck->amount+$request->amount;
                    $bitcheck->save();
                   $bit = $bitcheck;
                }else{
                    #get the requested data in Data variable
                    $data = [
                            'stores_id'     => $request->stores_id,
                            'game_id'       => $request->game_id ,
                            'day_id'        => $request->day_id  ,
                            'open_time'        => $request->open_time  ,
                            'date'          => now() ,
                            'type'          => $request->type ,
                            'amount'        => $request->amount*count($request->bit_place) ,
                            'user_id'       => auth('api')->user()->id ,
                           
                            ];
                    #store the newly creted resoures
                     $bit= $this->bit->create($data);
                }

               
                 foreach ($request->bit_place as $key => $bit_place) {
                      #get the requested data in Data variable
                        $check = $this->beadOnMotor->where('created_at','like',currentDate().'%')->where('bit_place',$key)->where('user_id',auth('api')->user()->id)->first();
                        if(!empty($check)){
                             $check->amount = $check->amount+$bit_place;
                             $check->save();
                        }else{
                             $data = [
                                'bit_id'          => $bit->id,
                                'bit_place'       => $key,
                                'amount'          => $bit_place , 
                                'user_id'         => auth('api')->user()->id ,
                               
                                ];
                        #store the newly creted resoures
                          $this->beadOnMotor->create($data); 
                        }
                      
                 }

                 DB::commit();
                $success['beadOnMotor'] =  '';

            return $this->sendResponse($success, ' successfully.');
        }catch(\Exception $ex){
            DB::rollback();
             return $this->sendError('error.', $ex->getMessage());
                
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
