<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreSliderRequest;
use App\Http\Requests\UpdateSliderRequest;
use App\Models\Slider;
use App\Services\SliderService;

class SliderController extends Controller
{
    private SliderService $sliderService;

    public function __construct(SliderService $sliderService)
    {
        $this->sliderService = $sliderService;
    }

    /**
     * List all sliders — title, image preview, status, order.
     * Requirement 7.3
     */
    public function index()
    {
        $sliders = $this->sliderService->getAll();

        return view('admin.sliders.index', compact('sliders'));
    }

    /**
     * Show the create form.
     * Requirement 7.4
     */
    public function create()
    {
        return view('admin.sliders.create');
    }

    /**
     * Persist a new slider via SliderService.
     * Requirement 7.5, 7.6
     */
    public function store(StoreSliderRequest $request)
    {
        $this->sliderService->create($request->validated());

        return redirect()
            ->route('admin.sliders.index')
            ->with('success', 'Slider created successfully.');
    }

    /**
     * Show the edit form pre-populated with the slider's data.
     * Requirement 7.7
     */
    public function edit(Slider $slider)
    {
        return view('admin.sliders.edit', compact('slider'));
    }

    /**
     * Update the slider via SliderService.
     * Requirement 7.8
     */
    public function update(UpdateSliderRequest $request, Slider $slider)
    {
        $this->sliderService->update($slider, $request->validated());

        return redirect()
            ->route('admin.sliders.index')
            ->with('success', 'Slider updated successfully.');
    }

    /**
     * Delete the slider via SliderService.
     * Requirement 7.9
     */
    public function destroy(Slider $slider)
    {
        $this->sliderService->delete($slider);

        return redirect()
            ->route('admin.sliders.index')
            ->with('success', 'Slider deleted successfully.');
    }
}
