<?php

namespace App\Http\Controllers\FrontControllers;

use App\Http\Controllers\Controller;

use App\Repositories\CategoryRepository;
use App\Repositories\CMSPagesRepositoryEloquent;
use App\Repositories\FaqRepository;
use App\Repositories\GalleryRepositoryEloquent;
use App\Repositories\HomePageSliderRepository;

use App\Repositories\ProductRepository;

use Illuminate\Http\Request;

class FrontController extends Controller
{

    private $cmsPageRepository;
    private $faqRepo;
    private $sliderRepo;
    private $productRepo;
    private $galleryRepo;

    private $categoryRepo;

    public function __construct(
        CMSPagesRepositoryEloquent $cMSPagesRepository,
        CategoryRepository $categoryRepository,
        FaqRepository $faqRepository,
        HomePageSliderRepository $homePageSliderRepository,
        ProductRepository $productRepository,
        GalleryRepositoryEloquent $galleryRepositoryEloquent

    ) {
        $this->cmsPageRepository = $cMSPagesRepository;
        $this->faqRepo = $faqRepository;
        $this->sliderRepo = $homePageSliderRepository;
        $this->productRepo = $productRepository;
        $this->galleryRepo = $galleryRepositoryEloquent;
        $this->categoryRepo = $categoryRepository;
    }
    public function index()
    {
        $data = $this->cmsPageRepository->getPageBySlug('home');
        $includeCMS = $this->cmsPageRepository->getPageBySlug('home');
        $includeGal = $this->cmsPageRepository->getPageBySlug('home');

        $gallery = $this->galleryRepo->getAllSliders();
        $sliders = $this->sliderRepo->getAllSliders();
        $categories = $this->categoryRepo->getAllCategories();
        $products = $this->productRepo->getAllProducts();
        return view('front.index', compact('products', 'categories', 'gallery', 'data', 'sliders','includeCMS','includeGal'));
    }
    public function about()
    {
        $data = $this->cmsPageRepository->getPageBySlug('about');
        $includeCMS = $this->cmsPageRepository->getPageBySlug('home');
        return view('front.about', compact('data','includeCMS'));
    }

    public function customerService()
    {
        $data = $this->cmsPageRepository->getPageBySlug('privacy');
        return view('front.customer-service', compact('data'));
    }

    public function customerReviews()
    {
        $data = $this->cmsPageRepository->getPageBySlug('privacy');
        return view('front.customer-reviews', compact('data'));
    }
    public function returnPolicy()
    {
        $data = $this->cmsPageRepository->getPageBySlug('policy');
        return view('front.return-policy', compact('data'));
    }


    public function luxuryCandles()
    {
        $data = $this->cmsPageRepository->getPageBySlug('luxury-candles');

        $categories = $this->categoryRepo->getAllCategories();
        $products = $this->productRepo->getAllProducts();

        return view('front.luxury-candles', compact('data', 'categories', 'products'));
    }

    public function faq()
    {
        $data = $this->cmsPageRepository->getPageBySlug('faq');
        $gallery = $this->galleryRepo->getAllSliders();
        $includeGal = $this->cmsPageRepository->getPageBySlug('home');

        $faqs = $this->faqRepo->getAllfaqs();

        return view('front.faq', compact('data','gallery','includeGal','faqs'));
    }

    public function delivery()
    {
        $data = $this->cmsPageRepository->getPageBySlug('delivery');

        return view('front.delivery', compact('data'));
    }

    public function productDetail($id)
    {
        $data = $this->cmsPageRepository->getPageBySlug('detail');
        try {
            $find = $this->productRepo->getById($id);
            $colourNames = $find->colors->pluck('name')->toArray();
            $colors = $find->colors->pluck('name', 'id')->toArray();

            return view('front.product-detail', compact('find', 'colourNames', 'colors', 'data'));
        } catch (\Exception $e) {
         
            return redirect()->route('front.index')->with('error', 'Product not Found');
        }

    }

}
