<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\HomePageHero;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class HomePageSettingController extends Controller
{

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


    function index()
    {
        $data = HomePageHero::first();
        return view('admin.home.herosection', compact('data'));
    }

    function herosectionStore(Request $request)
    {
        $request->validate([
            'first_words'=>['required'],
            'title' => 'nullable',
            'description' => 'nullable',
            'companies' => 'nullable',
            'countries' => 'nullable',
            'video_section' => ['nullable']
        ]);

        $homepagedata =   HomePageHero::query()->updateOrCreate([
            'id' => 1
        ], [
            'first_words'=>$request->first_words,
            'title' => $request->title,
            'description' => $request->description,
            'companies' => $request->companies,
            'countries' => $request->countries,
        ]);

        if ($request->hasFile('video_section')) {
            if(File::exists($homepagedata->video_section)){
                File::delete($homepagedata->video_section);
            }

            $homepagedata->video_section =  fileupload($request->file('video_section'), 'homepage-files');
            $homepagedata->save();
        }

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