<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateSettingHomeSectionsRequest;
use App\Http\Requests\UpdateSettingRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use App\Services\SettingService;
use Exception;

class SettingController extends Controller
{
    protected SettingService $settingService;

    public function __construct(SettingService $settingService)
    {
        $this->settingService = $settingService;
    }

    public function edit() {
        return $this->settingService->displayView('dashboard.setting.update');
    }

    public function update(UpdateSettingRequest $request) {
        try {
            $this->settingService->updateSetting($request);
            Session::flash('success', 'Setting updated successfully');
        } catch(Exception $e) {
            Session::flash('error', 'something went wrong');
        }

        return Redirect::back();
    }

    public function editHomeSections() {
        return $this->settingService->displayView('dashboard.setting.home_sections');
    }

    public function updateHomeSections(UpdateSettingHomeSectionsRequest $request) {
        try {
            $this->settingService->updateHomeSection($request);
            Session::flash('success', 'Home sections updated successfully');
        } catch(Exception $e) {
            Session::flash('error', 'something went wrong');
        }

        return Redirect::back();
    }
}
