<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\WashDetail;
use App\Models\CurrencyMaster;
use App\Models\CarWashOwner;
use App\Models\BodyType;
use App\Models\WashCategoryBodyType;
use App\Models\WashCategory;
use App\Models\CarOwnerCar;
use App\Models\CarOwner;
use App\Models\OptionalExtra;
use App\Models\CwoPasscode;
use App\Models\CleanerWash;
use App\Models\WashTypeToWash;
use App\Models\Loyalty;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
// use Twilio\Rest\Client;

class CwoReportController extends Controller
{
    public function __construct(){
    }


    public function index(){
    }

    public function dailySummaryReport_withOutSplit(Request $request){
        $validate = Validator::make($request->all(), 
            [
                'cwo_id' => 'required'
            ]);
            
        if($validate->fails()){
            $errorString = implode(",",$validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        $today = date('Y-m-d');  
        
        /***************** total car washed starts *********************/ 
        $todayTotalCarWashed = WashDetail::where('cwo_id', $cwo_id)
            ->whereDate('date_washed', '=', $today)
            // ->where('status', 3)
            ->count(); 
        /***************** total car washed ends *********************/ 

        /***************** cash starts *********************/
        $todayRevenueInCash = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'cash')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInCash->toArray());exit;
        $todayRevenueInCash = $todayRevenueInCash[0]->total_revenue != '' ? $todayRevenueInCash[0]->total_revenue : 0;
        /***************** cash ends *********************/

        /***************** card starts *********************/
        $todayRevenueInCard = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            // ->where('wash_details.payment_details', 'card')
            ->where('wash_details.payment_details', 'like', '%' . 'card' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInCard->toArray());exit;
        $todayRevenueInCard = $todayRevenueInCard[0]->total_revenue != '' ? $todayRevenueInCard[0]->total_revenue : 0;
        /***************** card ends *********************/

        /***************** contract starts *********************/
        $todayRevenueInContract = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'like', '%' . 'contract' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInContract->toArray());exit;
        $todayRevenueInContract = $todayRevenueInContract[0]->total_revenue != '' ? $todayRevenueInContract[0]->total_revenue : 0;

        // $todayRevenueInCash = $todayRevenueInCash + $todayRevenueInContract;
        /******************* contract ends ************************/

        $getCurrency = CarWashOwner::leftJoin('currency_masters','currency_masters.id','=','cwo_master.currency_id')->select('currency_masters.code', 'currency_masters.symbol')->where('cwo_master.id', $cwo_id)->get();
        // echo "<pre>";print_r($getCurrency->toArray());exit;

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'todayTotalCarWashed' => $todayTotalCarWashed,
            'todayRevenueInCash' => "$todayRevenueInCash",
            'todayRevenueInCard' => "$todayRevenueInCard",
            'todayRevenueInContract' => "$todayRevenueInContract",
            'currency' => $getCurrency[0]->symbol,
        ]);

    }

    public function dailySummaryReport(Request $request){
        $validate = Validator::make($request->all(), 
            [
                'cwo_id' => 'required'
            ]);
            
        if($validate->fails()){
            $errorString = implode(",",$validate->messages()->all());
            return response()->json([
                'success' => 0,
                'message' => $errorString,
            ]);
        }

        $cwo_id = $request->input('cwo_id');
        $today = date('Y-m-d');  
        
        /***************** total car washed starts *********************/ 
        $todayTotalCarWashed = WashDetail::where('cwo_id', $cwo_id)
            ->whereDate('date_washed', '=', $today)
            // ->where('status', 3)
            ->count(); 
        /***************** total car washed ends *********************/ 

        /***************** cash starts *********************/
        $todayRevenueInCash = WashDetail::select(DB::raw('SUM(wash_details.cash_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            // ->where('wash_details.payment_details', 'cash')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInCash->toArray());exit;
        $todayRevenueInCash = $todayRevenueInCash[0]->total_revenue != '' ? $todayRevenueInCash[0]->total_revenue : 0;
        /***************** cash ends *********************/

        /***************** card starts *********************/
        $todayRevenueInCard = WashDetail::select(DB::raw('SUM(wash_details.card_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            // ->where('wash_details.payment_details', 'card')
            // ->where('wash_details.payment_details', 'like', '%' . 'card' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInCard->toArray());exit;
        $todayRevenueInCard = $todayRevenueInCard[0]->total_revenue != '' ? $todayRevenueInCard[0]->total_revenue : 0;
        /***************** card ends *********************/

        /***************** contract starts *********************/
        $todayRevenueInContract = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'like', '%' . 'contract' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInContract->toArray());exit;
        $todayRevenueInContract = $todayRevenueInContract[0]->total_revenue != '' ? $todayRevenueInContract[0]->total_revenue : 0;

        $todayRevenueInEft = WashDetail::select(DB::raw('SUM(wash_details.total_amount) as total_revenue'))
            ->join('cwo_master','cwo_master.id','=','wash_details.cwo_id')
            ->where('wash_details.cwo_id', $cwo_id)
            ->where('wash_details.payment_details', 'like', '%' . 'eft' . '%')
            ->whereDate('wash_details.date_washed', '=', $today)
            ->get();
        // echo "<pre>";print_r($todayRevenueInEft->toArray());exit;
        $todayRevenueInEft = $todayRevenueInEft[0]->total_revenue != '' ? $todayRevenueInEft[0]->total_revenue : 0;

        // $todayRevenueInCash = $todayRevenueInCash + $todayRevenueInContract;
        /******************* contract ends ************************/

        $getCurrency = CarWashOwner::leftJoin('currency_masters','currency_masters.id','=','cwo_master.currency_id')->select('currency_masters.code', 'currency_masters.symbol')->where('cwo_master.id', $cwo_id)->get();
        // echo "<pre>";print_r($getCurrency->toArray());exit;

        return response()->json([
            'success' => 1,
            'message' => 'Success',
            'todayTotalCarWashed' => $todayTotalCarWashed,
            'todayRevenueInCash' => "$todayRevenueInCash",
            'todayRevenueInCard' => "$todayRevenueInCard",
            'todayRevenueInContract' => "$todayRevenueInContract",
            'todayRevenueInEft' => "$todayRevenueInEft",
            'currency' => $getCurrency[0]->symbol,
        ]);

    }

}
