<?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\Why_choose_us_section;
use App\Services\WhyChooseUsService;
use Exception;

class WhyChooseUsSectionsController extends Controller
{
    protected $model;
    protected WhyChooseUsService $whyChooseUsService;

    public function __construct(WhyChooseUsService $whyChooseUsService)
    {
        $this->model = new Why_choose_us_section();
        $this->whyChooseUsService = $whyChooseUsService;
    }

    // show why choose us sections list page
    public function index()
    {
        $why_choose_us_sections = $this->whyChooseUsService->retrieve($this->model);
        return $this->whyChooseUsService->displayView('dashboard.why_choose_us.list', ['why_choose_us_sections' => $why_choose_us_sections]);
    }

    // Show the form for creating a new why_choose us section.
    public function create()
    {
        return $this->whyChooseUsService->displayView('dashboard.why_choose_us.create');
    }

    // Store a newly created why_choose us section.
    public function store(CreateSectionRequest $request)
    {
        try{
            $this->whyChooseUsService->store($this->model, $request->toArray());
            Session::flash('success', 'Why choose 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 why_choose us section.
    public function edit($id)
    {
        $section = $this->whyChooseUsService->show($this->model, $id);
        return $this->whyChooseUsService->displayView('dashboard.why_choose_us.update', ['section' => $section]);
    }

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

        return Redirect::back();
    }

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

        return Redirect::back();
    }
}
