<?php

namespace App\Services;

use App\Models\Slider;
use App\Traits\ImageFunctions;

class SliderService extends BaseServices
{
    use ImageFunctions;

    protected $model;

    public function __construct()
    {
        $this->model = new Slider();
    }

    public function storeSlide($data)
    {
        return $this->model->create([
            'image' => $this->storeImagePath($data->image, 'slides'),
            'title_en' => $data->title_en,
            'title_ar' => $data->title_ar,
            'description_en' => $data->description_en,
            'description_ar' => $data->description_ar,
            'order' => $data->order,
        ]);
    }

    public function updateSlide(int $id, $data)
    {
        $slide = $this->model->findOrFail($id);
        $slide->title_en = $data->title_en;
        $slide->title_ar = $data->title_ar;
        $slide->description_en = $data->description_en;
        $slide->description_ar = $data->description_ar;
        $slide->order = $data->order;
        if($data->image) {
            $slide->image = $this->replaceImage($slide->image, $data->image, 'slides');
        }

        if($slide->isDirty()) {
            $slide->save();
        }

        return $slide;
    }

    public function deleteSlide($id)
    {
        $slide = $this->model->findOrFail($id);
        $this->deleteIfExist($slide->image);
        $slide->delete();
    }
}
