<?php

namespace App\Http\Controllers\API;

# Models
use App\Models\NewBit;
use App\Models\NewStore;
use App\Models\NewStoreGame;
use App\Models\NewCyclePattiBit;

# 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 NewCyclePattiController extends BaseController
{
   #Bind NewBitNewBit model
    protected $bit;
    
    #Bind NewStore model
    protected $store;
    
    #Bind NewStoreGame model
    protected $storeGame;

     #Bind NewCyclePattiiBit model
    protected $cyclePattiiBit;


    /**
      * Defining the default constructor for controller
      *
      */
    public function __construct(
                                NewBit $bit,
                                NewStore $store,
                                 NewStoreGame $storeGame,
                                NewCyclePattiBit $cyclePattiiBit
                            )
    {
        $this->bit  = $bit;
        $this->store                = $store;
         $this->storeGame                = $storeGame;
        $this->cyclePattiiBit       = $cyclePattiiBit;
        
    }
    /**
     * 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',
                'pati_name' => 'required',
              'time_id' => 'required',
            ]);

            if($validator->fails()){
                return $this->sendError('Validation Error.', $validator->errors());       
            }
                $time = '';
                
                $storess = $this->storeGame->where('id',$request->time_id)->first();
                if($request->type=='open'){
                    $time = $storess->opent_time;
                    
                }else{
                      $time = $storess->close_time;
                }
                    if(timeComapre($time)){
                         return $this->sendError('Error.', "Time Out");   
                    }
             $totalamount = totalAmount(auth('api')->user()->id);
               $bittotalamout =0;
             
             $bittotalamout =   $request->amount ;
             
            if($bittotalamout>$totalamount){
                return $this->sendError('Error.', "You have not sufficient balance");    
            }else{
                
            }
            //  foreach($request->stores_id as $key=>$storesid){
             $bitcheck = $this->bit->where('stores_id',$request->stores_id)->where('game_id',$request->game_id)->where('time_id',$request->time_id)->where('user_id',auth('api')->user()->id)->where('type',$request->type)->where('created_at','like',currentDate().'%')->first();
                if(!empty($bitcheck)){
                    $bitcheck->amount = $bitcheck->amount+$request->amount;
                    $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'        => $request->amount ,
                            '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->pati_name as $key => $pati_name) {
                       #get the requested data in Data variable
                    $check = $this->cyclePattiiBit->where('created_at','like',currentDate().'%')->where('bit_id',$bits_id)->where('bit_place',$request->pati_name)->first();
                    if(!empty($check)){
                       $doublePattiBitid = $check;
                       $check->amount = $check->amount + $request->amount;
                       $check->save();
                    }else{
                        #get the requested data in Data variable
                        $data = [
                                'bit_id'     => $bits_id,
                                'bit_place'       => $request->pati_name, 
                                 'amount'        => $request->amount ,
                                'user_id'       => auth('api')->user()->id ,
                               
                                ];

                        #store the newly creted resoures
                         $doublePattiBitid= $this->cyclePattiiBit->create($data);
                    }
                    debitAmount(auth('api')->user()->id, $request->amount);
                    
         $storess = $this->store->whereId($request->stores_id)->first();
                              
                            $remark =  'Played '.$storess->name.' '.$request->type.'Cycle Patti '. $request->pati_name.' of amount '. $request->amount;
                            $PaymentRemark = PaymentRemark(auth('api')->user()->id,$remark, $request->amount);
                            PaymentRemarkBit($PaymentRemark->id,null,$bits_id);
                           
                 
                 DB::commit();
                $success['singleDigitBit'] =  $request->type;

            return $this->sendResponse($success, ' successfully.');
             }catch(\Exception $ex){
            DB::rollback();
             return $this->sendError('error.', $ex->getMessage());
                
        }
    }
    
 
    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)
    {
        //
    }
}
