<?php

namespace App\Http\Controllers\Admin;

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

class AgentHeroController extends Controller
{

    function __construct()
    {
        $this->middleware('role_or_permission:agent hero section', ['only' => ['index', 'update']]);
    }

    function index()
    {
        $data = AgentHero::first();
        return view('admin.agent.hero.index', compact('data'));
    }
    function update(Request $request)
    {
        $request->validate([
            'title' => 'nullable',
            'description' => ['required',],
            'image' => ['nullable', 'image'],
        ]);

        $data = [
            'title' => $request->title,
            'description' => $request->description,
        ];
        if ($request->hasFile('image')) {
            $data['image'] = fileupload($request->file('image'), 'homepage-files');
        }

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

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