<?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 App\Models\OtherServiceReport; // to show total_amount including other products
use App\Models\PreBooking;
use App\Models\LoyaltyScore;
use App\Models\LoyaltyScoreUsageLog;
use App\Models\LoyaltyScoreAddLog;
use App\Models\WashCategoryLoyaltyPoint;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
// use Twilio\Rest\Client;

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


    public function index(){
    }

    public function updateLoyaltyType(Request $request){
        // $wash = WashDetail::where('is_free_wash', 1)->where('loyalty_updated', 0)->orderBy('id', 'desc')->get();
        // $wash = WashDetail::where('is_free_wash', 1)->where('loyalty_updated', 0)->limit(50)->get();
        $wash = WashDetail::where('loyalty_updated', 0)->limit(1000)->get();
        // echo "<pre>";print_r($wash->toArray());exit;
        if($wash != ''){
            foreach ($wash as $d) {
                $record = WashDetail::find($d->id);
                echo "<pre>";print_r($record->toArray());
                $loyalty_type = $d->loyalty_type;
                // if($loyalty_type > 0) {
                //     $record->loyalty_updated = 1;   
                // }
                $is_free_wash = $d->is_free_wash;
                $discount = $d->discount;
                echo $discount . "<br>";
                if($is_free_wash == 1) {
                    if($discount != 'undefined'){
                        if($discount > 0){
                            echo "discount" . "<br>";
                            $record->loyalty_type = 2;  
                        } else {
                            echo "free wash" . "<br>";
                            $record->loyalty_type = 1;
                        }
                    } else {
                        echo "free wash" . "<br>";
                        $record->loyalty_type = 1;
                    }
                } else {
                    if($discount > 0){
                        if($discount != 'undefined'){
                            $record->loyalty_type = 2; 
                        }
                    }
                }
                $record->loyalty_updated = 1;
                $record->save(); 
            }
        }
        // exit;
    }

    public function prebookingUpdateLoyaltyType(Request $request){
        // $data = PreBooking::where('is_free_wash', 1)->where('loyalty_updated', 0)->orderBy('id', 'desc')->get();
        // $data = PreBooking::where('is_free_wash', 1)->where('loyalty_updated', 0)->limit(50)->get();
        $data = PreBooking::where('loyalty_updated', 0)->limit(100)->get();
        // echo "<pre>";print_r($data->toArray());exit;
        if($data != ''){
            foreach ($data as $d) {
                $record = PreBooking::find($d->id);
                echo "<pre>";print_r($record->toArray());
                $loyalty_type = $d->loyalty_type;
                // if($loyalty_type > 0) {
                //     $record->loyalty_updated = 1;   
                // }
                $is_free_wash = $d->is_free_wash;
                $discount = $d->discount;
                echo $discount . "<br>";
                if($is_free_wash == 1) {
                    if($discount != 'undefined'){
                        if($discount > 0){
                            echo "discount" . "<br>";
                            $record->loyalty_type = 2;  
                        } else {
                            echo "free wash" . "<br>";
                            $record->loyalty_type = 1;
                        }
                    } else {
                        echo "free wash" . "<br>";
                        $record->loyalty_type = 1;
                    }
                } else {
                    if($discount > 0){
                        if($discount != 'undefined'){
                            $record->loyalty_type = 2; 
                        }
                    }
                }
                $record->loyalty_updated = 1;
                $record->save(); 
            }
        }
        // exit;
    }

}
