<?php

namespace Wave\Http\Controllers\CMS;

use App\Http\Controllers\Controller;
use App\Models\SiteSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PrivacyPolicyController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $siteSetting = SiteSetting::latest()->first();
        return view('theme::cms.privacy-policy.index', [
            'siteSetting' => $siteSetting,
        ]);
    }

    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'privacy_policy_english' => 'required',
            'privacy_policy_chinese' => 'required',
        ]);
        if ($validator->fails()) {
            return redirect()->back()->with(['message' => __('Fill up all required fields!'), 'message_type' => 'danger', 'show_toast' => true]);
        }
        else {
            $siteSetting = SiteSetting::latest()->first();
            if ($siteSetting) {
                $siteSetting->update($request->all());
            }
            else {
                SiteSetting::create($request->all());
            }
    
            return redirect()->back()->with(['message' => __('Privacy policy has successfully updated!'), 'message_type' => 'success', 'show_toast' => true]);
        }
    }
}
