<?php

namespace App\Http\Controllers\API;

# Models
use App\Models\NewBit;
use App\Models\NewChoicePannaBit;

# 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 NewChoicePannaBitController extends BaseController
{
      #Bind NewBit model
    protected $bit;

     #Bind NewChoicePannaBit model
    protected $choicePannaBit;

     /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                NewBit $bit,
                                NewChoicePannaBit $choicePannaBit
                            )
    {
        $this->bit  = $bit;
        $this->choicePannaBit       = $choicePannaBit;
        
    }
    /**
     * 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',
                'panna_type' => 'required',
                'bit_place' => 'required',
                'amount' => 'required',
              'time_id' => '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('stores_id',$request->stores_id)->where('time_id',$request->time_id)->where('game_id',$request->game_id)->where('user_id',auth('api')->user()->id)->first();
                 if($request->panna_type==='sp'){
                       $typeamount = 12*$request->amount;
                    }else{
                       $typeamount = 10*$request->amount;
                    }
                    if(!empty($bitcheck)){
                        $bitcheck->amount = $bitcheck->amount+$typeamount;
                        $bitcheck->save();
                       $bits_id = $bitcheck->id;
                    }else{
                        #get the requested data in Data variable
                        $data = [
                                'stores_id'     => $request->stores_id,
                                'game_id'       => $request->game_id ,
                                'date'          => now() ,
                                'type'          => $request->type ,
                                'amount'        => $typeamount ,
                                'user_id'       => auth('api')->user()->id ,
                                'day_id'        => $request->day_id  ,
                                'open_time'        => $request->open_time  ,
                                'time_id'       => $request->time_id,
                               
                                ];
                        #store the newly creted resoures
                         $bits= $this->bit->create($data); 
                         $bits_id = $bits->id;
                    }
                    
                 foreach ($request->bit_place as $key => $bit_place) {
                     $check = $this->choicePannaBit->where('bit_place',$key)->where('user_id',auth('api')->user()->id)->where('type','=',$request->panna_type)->first();
                    
                      if(!empty($check)){
                             $check->amount = $check->amount+$bit_place;
                             $check->save();
                        }else{
                            #get the requested data in Data variable
                        $data = [
                                'bit_id'     => $bits_id,
                                'type'       => $request->panna_type,
                                'bit_place'    => $key,
                                'amount'       => $bit_place,
                                'user_id'       => auth('api')->user()->id ,
                               
                                ];
                        #store the newly creted resoures
                          $this->choicePannaBit->create($data);

                        }
                 }
                      
                 

                 DB::commit();
                $success['singleDigitBit'] =  $request->type;

            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)
    {
        //
    }
}
