<?php

namespace App\Repositories;

use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Models\Pages;
use Illuminate\Http\Request;

class CMSPagesRepositoryEloquent extends BaseRepository
{

    protected $cmsModel;
    public function __construct(Pages $cmsModel)
    {
        $this->cmsModel = $cmsModel;
    }
    public function model()
    {
        return Pages::class;
    }


    public function getPageBySlug($slug)
    {
        $page = $this->cmsModel->PageBySlug($slug)->first();

        if ($page) {
            $page->content = json_decode($page->content, true);
        }
        return $page;
    }

    public function updatePageBySlugWithMedia(Request $request)
    {
        $slug = $request->input('slug');
        $mediaCollections = $request->input('media_collections', []);

        $page = $this->cmsModel->PageBySlug($slug)->firstOrFail();

        $content = $request->except(['_token', '_method']);
        $content = array_filter($content);

        $page->update([
            'content' => json_encode($content),
        ]);

        foreach ($mediaCollections as $collectionName) {
            $this->addOrUpdateMedia($page, $request, $collectionName);
        }

        return $page;
    }

    public function addOrUpdateMedia(Pages $page, Request $request, $collectionName)
    {
        if ($request->hasFile($collectionName)) {
            if ($page->hasMedia($collectionName)) {
                $page->clearMediaCollection($collectionName);
            }

            $page->addMediaFromRequest($collectionName)->toMediaCollection($collectionName);
        }
    }

    public function getMediaUrls(Pages $page, $collectionName)
    {
        return $page->getMedia($collectionName)->map(function ($item) {
            return $item->getUrl();
        });
    }
    public function viewReturn($slug, $content, $page)
    {
        // dd($slug);
        switch ($slug) {
            case 'home':
                return view('admin.pages.home', compact('content', 'page', 'slug'));
                break;

            case 'about':
                return view('admin.pages.about', compact('content', 'page', 'slug'));
                break;

            case 'contact':
                return view('admin.pages.contact', compact('content', 'page', 'slug'));
                break;

            case 'cart':
                return view('admin.pages.cart', compact('content', 'page', 'slug'));
                break;

            case 'detail':
                return view('admin.pages.detail', compact('content', 'page', 'slug'));
                break;

            case 'luxury-candles':
                return view('admin.pages.luxury-candles', compact('content', 'page', 'slug'));
                break;


            case 'policy':
                return view('admin.pages.policy', compact('content', 'page', 'slug'));
                break;

            case 'delivery':
                return view('admin.pages.delivery', compact('content', 'page', 'slug'));
                break;

            case 'faq':
                return view('admin.pages.faq', compact('content', 'page', 'slug'));
                break;

            default:
                abort(404);
        }
    }


}