<?php


namespace App\Http\Controllers;
// use App\Controller\Controller;
use App\Follower;
use Shop\Carts\Cart;
use Shop\Offer\Offer;
use Shop\Brands\Brand;
use Shop\Sliders\Slider;
use App\ProductPromotion;
use Shop\Products\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Shop\SocialLinks\SocialLink;
use Foundation\Supports\BaseController;
use Shop\QuestionAnswer\QuestionAnswer;
use Shop\Products\Services\ProductService;
use Symfony\Component\HttpFoundation\Response;

class FrontendController extends Controller
{
    public function flash_product()
    {
        $array_test=array();
        $data['product_promotion'] = Offer::where('started_date','<=',Carbon::now())->where('end_date','>=',Carbon::now())->with('product_promotion')->first();
        if ($data['product_promotion']) {
            foreach ($data['product_promotion']->product_promotion as $key => $product_promotion) {
                array_push($array_test, [
                    'product_name' => $product_promotion->product->name,
                    'amount_difference' =>($product_promotion->product->price) - ($product_promotion->flash_amount),
                    'price' => $product_promotion->product->price,
                    'flash_amount' => $product_promotion->flash_amount,
                    'image' => $product_promotion->product->getImage(),
                    'Description' => $product_promotion->product->description,
                    'meta_description' => $product_promotion->product->meta_description,
                    'quantity' => $product_promotion->product->quantity,
                    'meta_title' => $product_promotion->product->meta_title,
                    'color_and_size' => $product_promotion->product->metas,
                    'sku' => $product_promotion->product->sku,
                    'Serial_Number' => $product_promotion->product->serial_no,
                    'is_shippable_free' => $product_promotion->product->is_shippable_free,
                    'status' => $product_promotion->product->status,
                ]);
            }
        $flash_product = collect($array_test)->sortBy('amount_difference')->reverse()->all();
            return response()->json([
                'message' => 'Data is fetched successfully',
                'Offer_Name'=>$data['product_promotion']->name,
                'data' =>$flash_product,
            ], 200);
        } else {
            return response()->json([
                'status' => [
                    "message" => "For now there is no flash products",
                    "code" => "ok",
                ],
            ]);
        }
    }

    public function social_link()
    {
        $data['social_link']=SocialLink::all();
        $array_test=array();
        foreach($data['social_link'] as $key=>$social_link)
        {
            array_push($array_test,[
                'facebook_link'=>$social_link->facebook_link,
                'youtube_link'=>$social_link->youtube_link,
                'twitter_link'=>$social_link->twitter_link,
                'google_link'=>$social_link->google_link,
                'linkedin_link'=>$social_link->linkedin_link,
            ]);
        }
        return response()->json([
            'message'=>'Data is fetched successfully',
            'data'=>$array_test
        ],200);
    }

    public function answer_ques(Request $request)
    {
        if ($request->isMethod('post')) {

            $request->validate([
                'answer'=>'required|max:300|min:2',
                'question_id'=>'required'
            ]);
            $question = QuestionAnswer::find($request->question_id);

            $question->update([
                'answer'=>$request->answer,
                'answer_by'=>auth()->id(),
            ]);
            $notification = [
                'status' => 200,
                'message' => 'Submitted Answer',
                'data' => $question,
            ];
            return response()->json($notification);

        }
    }

    public function questions(Request $request)
    {
        $request->validate([
               'product_id'=>'required'
        ]);
        $data['questions'] = QuestionAnswer::select('id','question_by','answer_by','question','product_id','answer','status','created_at')->where('product_id',$request->product_id)
        ->with('askBy','answerBy','products')
        ->paginate(10);
        $array_test=array();
        foreach($data['questions'] as $key=>$questions_answer)
        {
            array_push($array_test,[
                'id'=>$questions_answer->id,
                'Question By'=>$questions_answer->question_by,
                'Answer By'=>$questions_answer->answer_by,
                'Question'=>$questions_answer->question,
                'Answer'=>$questions_answer->answer,
                'product id'=>$questions_answer->product_id,
                'Status'=>$questions_answer->status,
                'Created at'=>$questions_answer->create_at,
            ]);
        }
        return response()->json([
            'message'=>'Data is fetched successfully',
            'data'=>$array_test
        ],200);
    }

    public function ask_question(Request $request)
    {
        $fields=$request->validate([
            'question'=>'required|string',
            'question_by'=>'required|string',
            'product_id'=>'required|exists:products,id',
        ]);
        $auth_id=QuestionAnswer::create([
            'question'=>$fields['question'],
            'question_by'=>auth()->id(),
            'product_id'=>$fields['product_id'],
        ]);
        return response()->json([
            'message'=>'Data is Stored successfully',
            'data'=>$auth_id,
        ],200);
    }

    public function Brand_with_product()
    {
        $data['brand']=Brand::with('products')->get();
        return response()->json([
            'data'=> $data['brand']
        ],200);
    }

    public function free_delivery()
    {
        $data['free_delivery']=Product::where('is_shippable_free',0)->get();
        $array_test=array();
        foreach($data['free_delivery'] as $key=>$free_delivery)
        {
            array_push($array_test,[
                'product_name' => $free_delivery->name,
                'price' => $free_delivery->price,
                'sale_price' => $free_delivery->sale_price,
                'image' => $free_delivery->getImage(),
                'Description' => $free_delivery->description,
                'meta_description' => $free_delivery->meta_description,
                'quantity' => $free_delivery->quantity,
                'meta_title' => $free_delivery->meta_title,
                'color_and_size' => $free_delivery->metas,
                'sku' => $free_delivery->sku,
                'Serial_Number' => $free_delivery->serial_no,
                'is_shippable_free' => $free_delivery->is_shippable_free,
                'status' => $free_delivery->status,
            ]);
        }
        return response()->json([
            'message'=>'Data is fetched successfully',
            'data'=>$array_test
        ],200);
    }

    public function follow_unfollow(Request $request)
    {
        $fields=$request->validate([
            'followed_id'=>'required|exists:users,id'        
        ]);
        $follow=Follower::where([
            'followed_id'=>$request->followed_id,
            'follower_id'=>auth()->id(),
        ])->first();
        $un_follow=Follower::where([
            'followed_id'=>$request->followed_id,
            'follower_id'=>auth()->id(),
            'status'=>1,
        ])->first();
        if($follow === null)
        {
            $auth_id=Follower::create([
                'followed_id'=>$fields['followed_id'],
                'follower_id'=>auth()->id(),
                'status'=>1,
            ]);
            return response()->json([
                'message'=>'Your are followed Now',
                'data'=>$auth_id,
            ],200);      
        }
        else if($un_follow !==null)
        {
            $unfollow = $follow->update([
                'followed_id' => $fields['followed_id'],
                'follower_id'=>auth()->id(),
                'status'=>0,
            ]);
            return response()->json([
                'message' => 'You have unfollowed Now',
                'data' => $unfollow,
            ],200);  
        }      
      else
        {
         $data['product']=Product::where('vendor_id',$fields['followed_id'])->get();
            $unfollow = $follow->update([
                'followed_id' => $fields['followed_id'],
                'follower_id'=>auth()->id(),
                'status'=>1,
            ]);
             return response()->json([
                'message'=>'You have followed Now',
                 'products' => $data['product'],
                'data'=>$unfollow,
            ],200);  
        }      
           
    }

    public function wishlist_group()
    {
        $data['wishlist']=Cart::where('added_by',Auth()->id())->where('cart_type',1)->get();

        $grouped = $data['wishlist']->groupBy(function ($item, $key) {
            return $item->product->vendor_id;
        });
        return response()->json([
            'message'=>'WishList Products are  fetched successfully',
            'data'=>$grouped
        ],200);
    }

   
    public function cart_group()
    {
        $data['cart']=Cart::where('added_by',Auth()->id())->where('cart_type',0)->with('product.vendor')->get();
    //  return $data['cart'];
        $grouped_by_cart = $data['cart']->groupBy(function ($item, $key) {
            return $item->product->vendor_id;
        });
        return response()->json([
            'message'=>'Cart Group  is  fetched successfully',
            'data'=>$grouped_by_cart,
        ],200);
    }

    public function cart_group_delete()
    {
        $data['cart']=Cart::where('added_by',Auth()->id())->where('cart_type',0)->with('product.vendor')->get();
            $grouped_by_cart = $data['cart']->groupBy(function ($item, $key) {
                return $item->product->vendor_id->delete();
            });
            return response()->json([
                'message'=>'Cart is successfully deleted',
            ],200);
    }

    public function wishlist_group_delete()
    {
        $data['cart']=Cart::where('added_by',Auth()->id())->where('cart_type',1)->with('product.vendor')->get();
        $grouped_by_cart = $data['cart']->groupBy(function ($item, $key) {
            return $item->product->vendor_id->delete();
        });
        return response()->json([
            'message'=>'Wishlist is successfully deleted',
        ],200);
    }
}