<?php

namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\ProductCoupon;
use Illuminate\Http\Request;
use App\Traits\ProductTrait;
use App\Models\BusinessInformation;

class PackageController extends Controller
{   
    use ProductTrait;

    public function index()
    {   
        $radius = BusinessInformation::where('client_id',Auth()->User()->id)->get();
        foreach($radius as $rad)
        {
            $radius_value = $rad->radius;
            $closer = $rad->closerid;
            $bid = $rad ->id;
        }
        $data = $this->show_products($radius_value);
        foreach($data as $product)
        {
            $updatedproduct  = $product->id;
            $oldprice = $product->price;
        }
        $updatecouponproduct = ProductCoupon::where('business_id',$bid)
        ->update(
            [
                'product_id' => $updatedproduct,
            ]
        );
        $updatebusinesspid = BusinessInformation::where('client_id',Auth()->User()->id)
        ->update([
            'pid' => $updatedproduct
        ]);
        $productcoupon = BusinessInformation::select()
            ->join('product_coupons', 'product_coupons.business_id', '=', 'business_information.id')
            ->join('products', 'products.id', '=', 'product_coupons.product_id')
            ->join('coupons', 'coupons.id', '=', 'product_coupons.coupon_id')
            ->join('users', 'users.id', '=', 'coupons.closer_id')
            ->join('closers', 'closers.user_id', '=', 'users.id')
            ->where('business_information.client_id', '=', Auth()->User()->id)
            ->select(
                'coupons.id AS couponid',
                'coupons.code',
                'coupons.discount_price',
                'product_coupons.new_discount_price',
                'product_coupons.id AS pid',
                'products.price',
                'products.id AS productid',
                'business_information.id AS bid'
            )
            ->get();
        $coupon_data = [];
        foreach($productcoupon as $pcdata)
        {
            $new_d_price = $pcdata->price - $pcdata->discount_price;
            $cid = $pcdata->couponid;
            $coupon_data[] = [
                'bid' => $pcdata->bid,
                'couponid' => $cid,
                'pc_id' => $pcdata->pid,
                'code' => $pcdata->code,
                'discount_price' => $pcdata->discount_price,
                'price' => $pcdata->price,
                'productid' => $pcdata->productid,
                'new_discount_price' => $new_d_price
            ];
        }
        $cd = $coupon_data; 
        // dd($cd); 
        $data = $this->show_products($radius_value);
        if(!is_null($cd))
        {
            
            return view('packages')->with(['data'=>$data,'closer'=>$closer,'bid'=>$bid,'coupon'=>$cd]);
        }
        else{
            return view('packages')->with(['data'=>$data,'closer'=>$closer,'bid'=>$bid]);
        }
    }

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