<?php

namespace App\Services;

use App\Models\Service;
use App\Traits\ImageFunctions;

class ServicesService extends BaseServices
{
    use ImageFunctions;

    protected $model;

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

    public function storeService($data)
    {
        return $this->model->create([
            'name_en' => $data->name_en,
            'name_ar' => $data->name_ar,  
            'mini_description_en' => $data->mini_description_en,
            'mini_description_ar' => $data->mini_description_ar,          
            'description_en' => $data->description_en,
            'description_ar' => $data->description_ar,
            'image' => $this->storeImagePath($data->image, 'services'),
            'order' => $data->order,
            'page_details' => $data->page_details ? 1 : 0,
        ]);
    }

    public function updateService(int $id, $data)
    {
        $service = $this->model->findOrFail($id);
        $service->name_en = $data->name_en;
        $service->name_ar = $data->name_ar;
        $service->mini_description_en = $data->mini_description_en;
        $service->mini_description_ar = $data->mini_description_ar;
        $service->description_en = $data->description_en;
        $service->description_ar = $data->description_ar;
        $service->order = $data->order;
        $service->page_details = $data->page_details ? 1 : 0;

        if($data->image) {
            $service->image = $this->replaceImage($service->image, $data->image, 'services');
        }

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

        return $service;
    }

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