<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ClintReview;
use Illuminate\Http\Request;

class ClintReviewController extends Controller
{

    function __construct()
    {
        $this->middleware('role_or_permission:homepage clint review', ['only' => ['index', 'update']]);
    }

    function index(Request $request)
    {
        $data = ClintReview::query()->first();
        return view('admin.home.review.index', compact('data'));
    }

    function update(Request $request)
    {
        $request->validate([
            'title' => 'nullable',
            'clint_image' => ['nullable', 'image'],
            'clint_name' => 'nullable',
            'clint_position' => 'nullable'
        ]);

        $dataToUpdate = [
            'title' => $request->title,
            'clint_name' => $request->clint_name,
            'clint_position' => $request->clint_position
        ];

        // Check if clint_image is not null before attempting to upload
        if ($request->hasFile('clint_image')) {
            $dataToUpdate['clint_image'] = fileupload($request->file('clint_image'), 'homepage-files');
        }

        ClintReview::query()->UpdateOrCreate(['id' => 1], $dataToUpdate);

        $notification = array(
            'messege' => 'Updated successfully',
            'alert-type' => 'success'
        );
        return back()->with($notification);
    }
}
