<?php

namespace App\Services;

use App\Http\Requests\AboutUsRequest;
use App\Models\AboutUsSetting;
use Dipokhalder\EnvEditor\EnvEditor;
use Exception;
use Illuminate\Support\Facades\Log;
use Dipokhalder\Settings\Facades\Settings;

class AboutUsService
{
    public $envService;

    public function __construct(EnvEditor $envEditor)
    {
        $this->envService = $envEditor;
    }

    /**
     * @throws Exception
     */
    public function list()
    {
        try {
            return Settings::group('about_us')->all();
        } catch (Exception $exception) {
            Log::info($exception->getMessage());
            throw new Exception($exception->getMessage(), 422);
        }
    }

    /**
     * @throws Exception
     */
    public function update(AboutUsRequest $request)
    {
        try {
            Settings::group('about_us')->set($request->validated());
            if ($request->about_us_image) {
                $setting = AboutUsSetting::where('key', 'about_us_image')->first();
                $setting->clearMediaCollection('about-us');
                $setting->addMediaFromRequest('about_us_image')->toMediaCollection('about-us');
            }
            return $this->list();
        } catch (Exception $exception) {
            Log::info($exception->getMessage());
            throw new Exception($exception->getMessage(), 422);
        }
    }
}