<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateSectionRequest;
use App\Http\Requests\UpdateSectionRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use App\Models\About_us_section;
use App\Services\AboutUsService;
use Exception;

class AboutUsSectionsController extends Controller
{
    protected $model;
    protected AboutUsService $aboutUsService;

    public function __construct(AboutUsService $aboutUsService)
    {
        $this->model = new About_us_section();
        $this->aboutUsService = $aboutUsService;
    }

    // show about_us_sections list page
    public function index()
    {
        $about_us_sections = $this->aboutUsService->retrieve($this->model);
        return $this->aboutUsService->displayView('dashboard.about_us.sections.list', ['about_us_sections' => $about_us_sections]);
    }

    // Show the form for creating a new about us section.
    public function create()
    {
        return $this->aboutUsService->displayView('dashboard.about_us.sections.create');
    }

    // Store a newly created about us section.
    public function store(CreateSectionRequest $request)
    {
        try{
            $this->aboutUsService->store($this->model, $request->toArray());
            Session::flash('success', 'About us section created successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }

    // Display the specified resource.
    public function show($id)
    {
        //
    }

    //Show the form for editing the specified about us section.
    public function edit($id)
    {
        $section = $this->aboutUsService->show($this->model, $id);
        return $this->aboutUsService->displayView('dashboard.about_us.sections.update', ['section' => $section]);
    }

    // Update the specified about us section.
    public function update(UpdateSectionRequest $request, $id)
    {
        try{
            $this->aboutUsService->update($this->model, $id, $request->toArray());
            Session::flash('success', 'About us section updated successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }

    // Remove the specified about_us_section.
    public function destroy($id)
    {
        try{
            $this->aboutUsService->delete($this->model, $id);
            Session::flash('success', 'About us section deleted successfully');
        } catch(Exception $e) {
            Session::flash('error', 'Something went wrong');
        }

        return Redirect::back();
    }
}
