<?php

namespace App\Http\Controllers;
use App\Models\Coupon;
use App\Models\Closer;
use App\Models\Product;
use App\Models\ProductCoupon;
use App\Models\BusinessInformation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CouponController extends Controller
{
    public function check_coupon(Request $request)
    {
        if($request->ajax())
        {
        $business = BusinessInformation::where('client_id',Auth()->User()->id)->get();
        foreach($business as $bsns)
        {
            $bid = $bsns->id;
        }
        $productcoupon = ProductCoupon::select()
                        ->join('business_information','business_information.id','=','product_coupons.business_id')
                        ->join('coupons','coupons.id','=','product_coupons.coupon_id')
                        ->join('products','products.id','=','product_coupons.product_id')
                         ->where('product_coupons.business_id','=',$bid)
                         ->where('product_coupons.product_id','=',$request->productid)
                         ->select('product_coupons.business_id','product_coupons.product_id')
                         ->exists();
        if($productcoupon)
        {
            return Response('exist');
        }else{
            $output="true";
            $product = Product::where('id',$request->productid)->get();
            foreach($product as $data)
            {
                $productid = $data->id;
                $oldprice = $data->price;
            }
    
            $user = Closer::where('id',$request->clsr)->get('user_id');
            foreach($user as $id)
            {
                $uid = $id->user_id;
            }
            $coupon = Closer::select()
            ->join('business_information','business_information.closerid','=','closers.id')
            ->join('users','users.id','=','closers.user_id')
            ->join('coupons','coupons.closer_id','=','users.id')
            ->where('closers.user_id','=',$uid)
            ->where('coupons.code','=',$request->coupon)
            ->select([
            'coupons.id AS couponid',
            'coupons.code AS code',
            'coupons.discount_price AS discount_price',
            'closers.closer_name AS closer_name',
            'closers.id AS closerid',
            'users.id AS closer_user_id'
            ])
            ->get();
            if($coupon->isNotEmpty())
            {
            foreach($coupon as $code)
            {
            $newprice = $oldprice - $code->discount_price;
            }
            if($oldprice < $code->discount_price)
            {
                return Response('greaterprice');
            }
            else{
                return Response()->json([
                    'couponid' => $code->couponid,
                    'productid' => $productid,
                    'code' => $code->code,
                    'oldprice' => $oldprice,
                    'discountprice' => $code->discount_price,
                    'new_price' => $newprice,
                    'closerid' => $code->closer_user_id
                    ],200);
            }
            }else{
                return Response('invalid coupon');
            }
        }
       
        }
    }

    public function add_product_coupon(Request $request)
    {
        $data = [
            'coupon_id' => $request->couponid,
            'product_id' => $request->pid,
            'new_discount_price' => $request->newprice,
            'business_id' => $request->bid
        ];
        $packageupdate = BusinessInformation::where('client_id',Auth()->user()->id)
                                        ->update([
                                            'pid' => $request->pid,
                                        ]);
        $product = ProductCoupon::select()
                                  ->join('business_information','business_information.id','=','product_coupons.business_id')
                                  ->where('product_coupons.business_id','=',$request->bid)
                                //   ->where('product_coupons.product_id','=',$request->pid)
                                  ->exists();

        $price = Product::where('id',$request->pid)->get();
        foreach($price as $prdct)
        {
            $product_price = $prdct->price;
        }
        if ($product) {
            return redirect('/select-package')->with('error',"coupon already applied.");
        }
        if ($product_price < $request->newprice) {
            return redirect('/select-package')->with('error',"Product price should be greater than Coupon price.");
        }
        else{
            
            $productCoupon = ProductCoupon::create($data);
            $business = BusinessInformation::select()
            ->join('product_coupons','product_coupons.business_id','=','business_information.id')
            ->join('coupons','coupons.id','=','product_coupons.coupon_id')
            ->join('products','products.id','=','product_coupons.product_id')
            ->join('closers','closers.id','=','business_information.closerid')
            ->join('users','users.id','=','business_information.client_id')
            ->where('business_information.client_id','=',Auth()->User()->id)
            ->select(
                'business_information.bname AS bname',
                'business_information.bemail AS bemail',
                'business_information.bphone AS bphone', 
                'business_information.baddress AS baddress',
                'business_information.website_url', 
                'business_information.radius_address',
                'business_information.radius AS bradius',
                'business_information.services AS services',
                'products.productname AS productname', 
                'products.price AS price','products.productdescription AS pdesc',
                'users.first_name','users.last_name',
                'users.email','users.phone',
                'closers.closer_name')
            ->get();
            return view('/order')->with('data',$business);
        }
    }

    public function remove_coupon($couponid)
    {
        $coupon = ProductCoupon::where('id','=', $couponid)->delete();
        return redirect('/checkout')->with('message','coupon code removed successfully.');
    }

    public function update_product_coupon(Request $request)
    {
        $product_coupon = ProductCoupon::where('id',$request->pid)
                          ->update([
                            'coupon_id' => $request->couponid,
                            'product_id' => $request->productid,
                            'new_discount_price' => $request->newprice,
                            'business_id' => $request->bid
                          ]);
                          $business = BusinessInformation::select()
                          ->join('product_coupons','product_coupons.business_id','=','business_information.id')
                          ->join('coupons','coupons.id','=','product_coupons.coupon_id')
                          ->join('products','products.id','=','product_coupons.product_id')
                          ->join('closers','closers.id','=','business_information.closerid')
                          ->join('users','users.id','=','business_information.client_id')
                          ->where('business_information.client_id','=',Auth()->User()->id)
                          ->select(
                              'business_information.bname AS bname',
                              'business_information.bemail AS bemail',
                              'business_information.bphone AS bphone', 
                              'business_information.baddress AS baddress',
                              'business_information.website_url', 
                              'business_information.radius_address',
                              'business_information.radius AS bradius',
                              'business_information.services AS services',
                              'products.productname AS productname', 
                              'products.price AS price','products.productdescription AS pdesc',
                              'users.first_name','users.last_name',
                              'users.email','users.phone',
                              'closers.closer_name')
                          ->get();
        return view('/order')->with('data',$business);
    }
}
