<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Contacts;
use App\Models\Faq;
use App\Models\Pages;
use App\Classes\GeniusMailer;
use App\Repositories\CMSPagesRepositoryEloquent;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class PagesController extends Controller
{

    protected $cmsPagesRepository;

    public function __construct(CMSPagesRepositoryEloquent $cmsPagesRepository)
    {
        $this->cmsPagesRepository = $cmsPagesRepository;
    }
    public function edit($slug)
    {
        try {
            $page = $this->cmsPagesRepository->getPageBySlug($slug);
            $content = $page->content;

            return $this->cmsPagesRepository->viewReturn($slug, $content, $page);
        } catch (\Exception $e) {
            return redirect()->back()->with('error', $e->getMessage());
        }
    }

    public function editAndUpdate(Request $request, $slug)
    {
        try {
            DB::beginTransaction();
            $page = $this->cmsPagesRepository->getPageBySlug($slug);
            if (!$page) {
                abort(404);
            }
            $this->cmsPagesRepository->updatePageBySlugWithMedia($request);
            $message = $slug . 'Page Update Successfully';
            DB::commit();
            return redirect()->back()->with('success', $message);
        } catch (\Exception $e) {
            DB::rollback();
            return redirect()->back()->with('error', $e->getMessage());
        }
    }
}
