<?php

namespace App\Http\Controllers\Backend;

use App\Helpers\CoreConstants;
use App\Http\Controllers\Controller;
use App\Models\Slider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class SliderController extends Controller
{
    public function index()
    {
        $data['PageTitle']             = 'Header Content';
        $data['sliders']               = Slider::orderByDesc('id')->get();
        $data['parentSliderShowClass'] = 'show';
        $data['SliderActiveClass']     = 'active';

        return view('backend.slider.index')->with($data);
    }

    public function create()
    {
        $data['PageTitle']             = 'Add Header Content';
        $data['parentSliderShowClass'] = 'show';
        $data['SliderActiveClass']     = 'active';

        return view('backend.slider.create')->with($data);
    }

    public function store(Request $request)
    {
        $request->validate([
            'type'        => 'required|in:large,small',
            'title'       => 'required|string|max:255',
            'description' => 'required|string',
            'youtube_url' => 'required|url|max:1000',
            'img_info'    => 'nullable|string|max:255',
            'image'       => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
        ]);

        $slider              = new Slider();
        $slider->type        = $request->type;
        $slider->title       = $request->title;
        $slider->description = $request->description;
        $slider->youtube_url = $request->youtube_url;
        $slider->alt         = $request->img_info;

        if ($request->hasFile('image')) {
            $slider->image = $request->file('image')->store('slider', 'public');
        }

        $slider->save();

        return redirect()
            ->route('admin.slider.index')
            ->with('success', CoreConstants::CREATED_SUCCESSFULLY);
    }

    public function edit($id)
    {
        $data['PageTitle']             = 'Edit Header Content';
        $data['parentSliderShowClass'] = 'show';
        $data['SliderActiveClass']     = 'active';
        $data['slider']                = Slider::findOrFail($id);

        return view('backend.slider.edit')->with($data);
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'type'        => 'required|in:large,small',
            'title'       => 'required|string|max:255',
            'description' => 'required|string',
            'youtube_url' => 'required|url|max:1000',
            'img_info'    => 'nullable|string|max:255',
            'image'       => 'nullable|image|mimes:jpeg,png,jpg,webp|max:2048',
        ]);

        $slider              = Slider::findOrFail($id);
        $slider->type        = $request->type;
        $slider->title       = $request->title;
        $slider->description = $request->description;
        $slider->youtube_url = $request->youtube_url;
        $slider->alt         = $request->img_info;

        if ($request->hasFile('image')) {
            if ($slider->image && Storage::disk('public')->exists($slider->image)) {
                Storage::disk('public')->delete($slider->image);
            }

            $slider->image = $request->file('image')->store('slider', 'public');
        }

        $slider->save();

        return redirect()
            ->route('admin.slider.index')
            ->with('success', CoreConstants::UPDATED_SUCCESSFULLY);
    }

    public function destroy($id)
    {
        $slider = Slider::findOrFail($id);

        if ($slider->image && Storage::disk('public')->exists($slider->image)) {
            Storage::disk('public')->delete($slider->image);
        }

        $slider->delete();

        return redirect()
            ->route('admin.slider.index')
            ->with('success', CoreConstants::DELETED_SUCCESSFULLY);
    }
}
