<?php

namespace App\Services;

use App\Models\Setting;
use App\Traits\ImageFunctions;

class SettingService extends BaseServices
{
    use ImageFunctions;

    protected $model;

    public function __construct() {
        $this->model = new Setting();
    }

    public function updateSetting($request) {
        Setting::where('name', 'name')->update(['value_en' => $request->name_en, 'value_ar' => $request->name_ar]);
        Setting::where('name', 'email')->update(['value_en' => $request->email]);
        Setting::where('name', 'phone')->update(['value_en' => $request->phone]);
        Setting::where('name', 'address')->update(['value_en' => $request->address_en, 'value_ar' => $request->address_ar]);
        Setting::where('name', 'location')->update(['value_en' => $request->location]);


        if($request->icon) {
            $icon = Setting::where('name', 'icon')->first();
            $icon->value_en = $this->replaceImage($icon->value_en, $request->icon, 'setting', 'icon');
            $icon->save();
        }

        if($request->logo_en) {
            $logo = Setting::where('name', 'logo')->first();
            $logo->value_en = $this->replaceImage($logo->value_en, $request->logo_en, 'setting', 'logo_en');
            $logo->save();
        }

        if($request->logo_ar) {
            $logo = Setting::where('name', 'logo')->first();
            $logo->value_ar = $this->replaceImage($logo->value_ar, $request->logo_ar, 'setting', 'logo_ar');
            $logo->save();
        }
    }

    public function updateHomeSection($request) {
        Setting::where('name', 'services')->update(['value_en' => $request->services_en, 'value_ar' => $request->services_ar]);
        Setting::where('name', 'why_choose_us')->update(['value_en' => $request->why_choose_us_en, 'value_ar' => $request->why_choose_us_ar]);
        Setting::where('name', 'about_us')->update(['value_en' => $request->about_us_en, 'value_ar' => $request->about_us_ar]);
    }
}
